The typeof
operator in JavaScript returns the data type of a variable or an expression. It is commonly used to determine the type of a given value.
Primitive Data Types
In JavaScript, primitive values are single, immutable data types without properties or methods. JavaScript has seven primitive data types:
string
number
boolean
bigint
symbol
null
undefined
Example
console.log(typeof "John"); // "string"
console.log(typeof ("John" + "Doe")); // "string"
console.log(typeof 3.14); // "number"
console.log(typeof 33); // "number"
console.log(typeof (33 + 66)); // "number"
console.log(typeof true); // "boolean"
console.log(typeof false); // "boolean"
console.log(typeof 1234n); // "bigint"
console.log(typeof Symbol()); // "symbol"
console.log(typeof x); // "undefined"
console.log(typeof null); // "object"
Note: In JavaScript, null
is a primitive value. However, typeof null
returns object due to a historical bug.
Complex Data Types
JavaScript has one primary complex data type:
object
Example: Objects include various structures like arrays, functions, maps, and sets.
console.log(typeof {name: "John"}); // "object"
console.log(typeof [1, 2, 3, 4]); // "object"
console.log(typeof new Map()); // "object"
console.log(typeof new Set()); // "object"
console.log(typeof function(){}); // "function"
How to Identify an Array?
Example: To check if a variable is an array, use Array.isArray()
:
const fruits = ["apples", "bananas", "oranges"];
console.log(Array.isArray(fruits)); // true
The instanceof
Operator
Example: The instanceof
operator checks whether an object belongs to a specific class or type.
const time = new Date();
console.log(time instanceof Date); // true
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits instanceof Array); // true
const fruitMap = new Map([
["apples", 500],
["bananas", 300],
]);
console.log(fruitMap instanceof Map); // true
const fruitSet = new Set(["apples", "bananas", "oranges"]);
console.log(fruitSet instanceof Set); // true
Undefined Variables
Example: When a variable is declared but not assigned a value, its type is undefined
.
let car;
console.log(typeof car); // "undefined"
Example: Setting a variable to undefined
explicitly also results in the same type.
let vehicle = "Toyota";
vehicle = undefined;
console.log(typeof vehicle); // "undefined"
Empty Values vs. Undefined
Example: An empty string has a valid type (string
) and is different from undefined
.
let car = "";
console.log(typeof car); // "string"
Null and Objects
Example: In JavaScript, null
represents an intentional absence of value. However, its type is object
.
let person = { firstName: "John", lastName: "Doe" };
person = null;
console.log(typeof person); // "object"
Example: To set an object to an undefined state
let person = { firstName: "John", lastName: "Doe" };
person = undefined;
console.log(typeof person); // "undefined"
Example: Difference Between undefined
and null
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"
console.log(null === undefined); // false
console.log(null == undefined); // true
The constructor
Property
Example: The constructor
property identifies the constructor function of an object.
console.log({name:"John", age:34}.constructor); // function Object()
console.log([1,2,3,4].constructor); // function Array()
console.log(new Date().constructor); // function Date()
console.log(new Set().constructor); // function Set()
console.log(new Map().constructor); // function Map()
console.log(function() {}.constructor); // function Function()
Example: Using the constructor to check for data types:
console.log(myArray.constructor === Array); // true
console.log(myDate.constructor === Date); // true
Note: The data type of NaN
(Not a Number) is number
.