JavaScript Typeof

The typeof operator in JavaScript is used to determine the type of a variable or an expression. It returns a string indicating the data type of the operand.

Here, we'll explore how typeof works with various data types and provide code examples for each scenario.

Basic Usage

The basic usage of the typeof operator involves passing a variable or an expression as an operand to determine its data type.


let x = 42;
console.log(typeof x);// Output: "number"

let y = "Hello, World";
console.log(typeof y);// Output: "string"

In the above example, typeof x returns "number" because xis a number, while typeof y returns "string" because yis a string.

Example


let x = 42;
console.log(typeof x);// Output: "number"

let y = "Hello, World";
console.log(typeof y);// Output: "string"

Handling Primitive Data Types

typeof can handle all primitive data types in JavaScript, including numbers, strings, booleans, undefined, and symbols.


let a = 10;
console.log(typeof a);// Output: "number"

let b = "Hello";
console.log(typeof b);// Output: "string"

let c = true;
console.log(typeof c);// Output: "boolean"

let d;
console.log(typeof d);// Output: "undefined"

let e = Symbol("foo");
console.log(typeof e);// Output: "symbol"

Each of these examples returns the corresponding data type when passed to typeof.

Example


let a = 10;
console.log(typeof a);// Output: "number"

let b = "Hello";
console.log(typeof b);// Output: "string"

let c = true;
console.log(typeof c);// Output: "boolean"

let d;
console.log(typeof d);// Output: "undefined"

let e = Symbol("foo");
console.log(typeof e);// Output: "symbol"

Objects and Functions

For objects and functions, typeofbehaves differently compared to primitive data types. It returns "object"for objects and function for functions.


let obj = {};
console.log(typeof obj);// Output: "object"

let func = function() {};
console.log(typeof func);// Output: "function"

In these cases, typeofprovides information about the general type of the operand rather than specific details about its structure.

Example


let obj = {};
console.log(typeof obj);// Output: "object"

let func = function() {};
console.log(typeof func);// Output: "function"

Handling Arrays

Arrays are special types of objects in JavaScript. When using typeofwith arrays, it returns "object"not "array".

Example


let arr = [1,2,3];
console.log(typeof arr);// Output: "object"

Example


let arr = [1,2,3];
console.log(typeof arr);// Output: "object"

To determine if a variable is an array, you can use Array.isArray()method instead.


let arr = [1,2,3];
console.log(Array.isArray(arr));// Output: true

Example


let arr = [1,2,3];
console.log(Array.isArray(arr));// Output: true

Handling Null

Surprisingly, typeof nullreturns "object". This is considered a quirk in JavaScript and has been present since the early versions of the language.


let nullVar = null;
console.log(typeof nullVar);// Output: "object"

Example


let nullVar = null;
console.log(typeof nullVar);// Output: "object"

To check if a variable is null, you can use a strict equality check "(===)" against null


let nullVar = null;
console.log(nullVar === null);// Output: true

Example


let nullVar = null;
console.log(nullVar === null);// Output: true

Understanding how typeofworks in JavaScript helps in better handling and manipulation of different data types within your codebase.