JavaScript typeof Operator
The JavaScript typeof operator returns the data type of a variable or expression. It’s a unary operator placed before its operand and returns a string indicating the data type, such as “number”, “string”, “boolean”, “object”, “undefined”, “function”, or “symbol”. It’s commonly used to dynamically determine the type of data being processed, facilitating conditional logic and type checking in JavaScript programs.
Syntax:
typeof operand
// OR
typeof (operand)
Primitive Data Types
Primitive data types in JavaScript are basic data types that represent single values. They include:
Data Type | Description |
---|---|
Number | Represents numeric values like integers and floating-point numbers. |
String | Represents textual data enclosed within single quotes (”) or double quotes (“”). |
Boolean | Represents true or false values. |
Undefined | Represents a variable that has been declared but has not been assigned a value. |
Null | Represents the intentional absence of any object value. |
Symbol | Represents a unique and immutable data type used as the key of an object’s property. |
BigInt | Represents large integers beyond the limit of the Number type. |
Example 1: Below example show the results of typeof operator for premitive data types.
// Define variables with different primitive data types
const num = 10;
const str = "Hello";
const bool = true;
const undef = undefined;
const nul = null;
const sym = Symbol("symbol");
const bigInt = 9007199254740991n;
// Use typeof operator to determine the data type
console.log(typeof num); // Output: "number"
console.log(typeof str); // Output: "string"
console.log(typeof bool); // Output: "boolean"
console.log(typeof undef); // Output: "undefined"
console.log(typeof nul); // Output: "object" (typeof null is an oddity,
// it returns "object")
console.log(typeof sym); // Output: "symbol"
console.log(typeof bigInt);// Output: "bigint"
Output
number string boolean undefined object symbol bigint
Example 2: This example uses ‘===’ (strict equality comparison operator) which compare value and type both and then return true or false.
//Number
console.log(typeof 25 === 'number');
console.log(typeof 3.14 === 'number');
console.log(typeof (69) === 'number');
// log base 10
console.log(typeof Math.LN10 === 'number');
console.log(typeof Infinity === 'number');
// Despite being "Not-A-Number"
console.log(typeof NaN === 'number');
// Wrapping in Number() function
console.log(typeof Number('100') === 'number');
Explanation: In the first console.log(), the js starts compiling from left to right and it first calculates the type of 25 which is ‘number’, and then compares it with ‘number’ and then finally returns true or false accordingly.
Output
true true true true true true true
Example 3: This example compares the function datatype using the typeof operator.
// function
console.log(typeof function () { } === 'function');
//classes too are objects
console.log(typeof class C { } === 'function');
console.log(typeof Math.sin === 'function');
Output
true true true