Function typeof
typeof Operator in JavaScript
The typeof operator in JavaScript is used to determine the type of a given variable or expression. It returns a string indicating the type of the unevaluated operand.
Syntax
typeof operand
operandcan be any expression whose type is to be determined.
Return Values
The typeof operator returns one of the following strings:
"undefined": if the variable is undefined"boolean": if the variable is a boolean"number": if the variable is a number"bigint": if the variable is a bigint"string": if the variable is a string"symbol": if the variable is a symbol"object": if the variable is an object (including null and arrays)"function": if the variable is a function
Examples
Basic Types
console.log(typeof undefined); // "undefined"
console.log(typeof true); // "boolean"
console.log(typeof 42); // "number"
console.log(typeof 9007199254740991n); // "bigint"
console.log(typeof "Hello, World!"); // "string"
console.log(typeof Symbol("id")); // "symbol"
Objects and Functions
console.log(typeof {name: "John", age: 30}); // "object"
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof function() {}); // "function"
console.log(typeof null); // "object" (this is a known quirk in JavaScript)
Undefined Variables
let a;
console.log(typeof a); // "undefined"
Checking Variables
let number = 123;
if (typeof number === 'number') {
console.log("This is a number.");
}
Special Cases
Null
Despite being an object, null is often considered a primitive value. The typeof operator returns "object" for null, which is a quirk of JavaScript.
console.log(typeof null); // "object"
Arrays
Arrays are a special kind of object in JavaScript.
let arr = [1, 2, 3];
console.log(typeof arr); // "object"
Functions
Functions are a subtype of objects, and the typeof operator returns "function" for them.
function myFunction() {}
console.log(typeof myFunction); // "function"
Symbols
Symbols are a relatively new primitive type introduced in ECMAScript 6 (ES6).
let sym = Symbol("foo");
console.log(typeof sym); // "symbol"
NaN
NaN stands for “Not-a-Number” and is a special numeric value representing an invalid number. The typeof operator returns "number" for NaN.
let notANumber = NaN;
console.log(typeof notANumber); // "number"
console.log(NaN === NaN); // false, because NaN is not equal to anything, including itself
Undefined
A variable that has been declared but not assigned a value is undefined. The typeof operator returns "undefined" for such variables.
let undefinedVar;
console.log(typeof undefinedVar); // "undefined"
console.log(typeof undefined); // "undefined"
Using typeof with Variables
The typeof operator can be particularly useful when working with variables whose type might change or is unknown.
let value;
value = 42;
console.log(typeof value); // "number"
value = "Hello";
console.log(typeof value); // "string"
value = true;
console.log(typeof value); // "boolean"
value = {};
console.log(typeof value); // "object"
value = function() {};
console.log(typeof value); // "function"
value = NaN;
console.log(typeof value); // "number"
value = undefined;
console.log(typeof value); // "undefined"