Javascript typeof operator

Created By: chatGPT

The typeof operator in JavaScript is used to determine the data type of a given variable. This operator returns a string indicating the type of the unevaluated operand. It's a useful tool for debugging, checking variable types, and making your code more robust. Here’s how it works:
The basic syntax is as follows:
typeof variable;
You can use typeof with different data types. Here are some examples:
let number = 42;
console.log(typeof number); // 'number'

let text = 'Hello, World!';
console.log(typeof text); // 'string'

let isActive = true;
console.log(typeof isActive); // 'boolean'

let notDefined;
console.log(typeof notDefined); // 'undefined'

let obj = {};
console.log(typeof obj); // 'object'

let func = function() {};
console.log(typeof func); // 'function'
Important notes on the typeof operator:
1. The result of typeof for arrays is 'object', not an array. Use Array.isArray() for array checks:
let arr = [1, 2, 3];
console.log(typeof arr); // 'object'
console.log(Array.isArray(arr)); // true
2. For null, the result is also 'object' due to a historical bug in JavaScript:
let nothing = null;
console.log(typeof nothing); // 'object'
3. The typeof operator returns a string with lowercase letters for types. Make sure to handle them correctly in conditions:
if (typeof number === 'number') {
    console.log('This is a number');
}
In summary, the typeof operator is a simple yet powerful way to perform type checking in JavaScript. Always remember its quirks, especially with arrays and null values, to avoid unexpected behaviors in your code.
Introduction And SetupVariablesData TypesMethodsControl FlowFunctionsOperatorsData StructuresCommentsSyntaxEvent HandlingError HandlingPrimitive Data TypesReference Data TypesConstLetVarStringNumberBooleanNullUndefinedSymbolObjectArrayTemplate LiteralsArrow FunctionsRegular FunctionsCallApplyBindAsync FunctionsPromisesCallback FunctionsIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor EachBreak StatementContinue StatementTry CatchThrowFinallyScopeClosureThis KeywordPrototypesInheritanceConstructor FunctionsClass SyntaxStatic MethodsGettersSettersSpread OperatorRest OperatorDestructuring AssignmentMap MethodFilter MethodReduce MethodEvent ListenersDom ManipulationInnerHTMLQuerySelectorAddEventListenerPreventDefaultStopPropagationTimeoutsIntervalsJsonLocal StorageSession StorageRegexMath ObjectDate ObjectString MethodsArray MethodsObject MethodsDeep CloningShallow CloningCallback HellPromise ChainingAsync AwaitMap Vs ForEachFilter Vs ReduceNullish Coalescing OperatorOptional ChainingTypeof OperatorInstanceof OperatorStrict EqualityLoose Equality