Learnitweb

typeof operator in JavaScript

1. Introduction

The typeof operator returns the type of the object or primitive operand. The return type of typeof is string. The typeof operator is very helpful but is not suitable for all situations. The typeof operator returns object for many objects so it can not be used precisely in the code to check the type. For example:

console.log(typeof []); // object
console.log(typeof null); // object
console.log(typeof new String("")); // object
console.log(typeof new Number(10));  // object

As you observe, the typeof operator returns object in all these cases. So it can not be used precisely to check the type of operator.

2. Syntax

The syntax to use typeof is:

typeof operand
typeof (operand)

You can use typeof without parentheses around operand but when you need to get type of an expression, you need to use parentheses. For example:

let num = 10;

console.log(typeof num + " hello"); // 'number hello'
console.log(typeof (num + " hello")); // 'string'

3. Result of typeof with primitive and object

Following is a summary of the result of using typeof with different primitive and object:

TypeResult
BigInt“bigint”
Boolean“boolean”
Function object“function”
Null“object”
Number“number”
String“string”
Symbol“symbol”
Function object“function”
Undefined“undefined”
any other object“object”

4. typeof with ‘new’ operator

The typeof operator when used with constructor functions, except Function constructor returns ‘object’.

let num = new Number(10);
let str = new String("hello");

console.log(typeof num); // result is 'object'
console.log(typeof str); // result is 'object' 

The typeof operator when used with Function constructor returns ‘function’.

let func = new Function();
console.log(typeof func); // result is 'function'

5. Error with typeof

The typeof operator throws ReferenceError when used with let, const or class when in temporal dead zone.

console.log(typeof x); // undefined
console.log(typeof a); // ReferenceError
console.log(typeof b); // ReferenceError
console.log(typeof test); // ReferenceError

let a = 0;
const b = 1;
class test {}

6. Examples of typeof

console.log(typeof 10); // number
console.log(typeof 10.1); // number
console.log(typeof 10); // number
console.log(typeof Infinity); // number
console.log(typeof NaN); // number
console.log(typeof Number("1")); // number

console.log(typeof "test"); // string
console.log(typeof typeof 1); // string
console.log(typeof "10"); // string

console.log(typeof true); // boolean
console.log(typeof false); // boolean
console.log(typeof Boolean(1)); // boolean

console.log(typeof new String("hello")); // object
console.log(typeof []); // object
console.log(typeof [1, 2, 3]); // object
console.log(typeof null); // object
console.log(typeof new Number(10)); // object
console.log(typeof new Boolean(true)); // object

console.log(typeof Symbol()); // symbol
console.log(typeof Symbol("test")); // symbol
console.log(typeof Symbol.iterator); // symbol

console.log(typeof undefined); // undefined

console.log(typeof function () {}); // function
console.log(typeof class C {}); // function

7. Conclusion

In this tutorial, we discussed typeof operator. A better understanding of this operator is essential to write bug free code. You should know that typeof operator can not be used in every situation to get the type of operand.

We hope this tutorial was helpful.