JavaScript - typeof operator

Introduction

Namaste, In this blog I will discuss the following topics

  1. What is typeof operator?

  2. Using typeof operator

  3. Complete source code

Video Tutorial

What is typeof Operator?

The typeof operator is used for finding out the type of an operand (variable/object). It is an unary operator i.e. it works with a single operand. The return value is a string which identifies the type.

typeof operand

Using typeof operator

The typeof operator returns a string identifying the type.

Below are some scenarios

  1. typeof with primitives

     let myval=23121212n;
     console.log("type:", typeof myval, " value:", myval);
     // result - type: bigint  value: 23121212n
    
     myval = undefined;
     console.log("type:", typeof myval, " value:", myval);
     //result - type: undefined  value: undefined
    
     myval = null;
     console.log("type:", typeof myval, " value:", myval);
     //result - type: object  value: null
    
     myval = Symbol("Key");
     console.log("type:", typeof myval, " value:", myval);
     //result - type: symbol  value: Symbol(Key)
    

    The type for null is returned as an object which is a bug in JS. It should noted that null is a primitive.

  2. typeof with Objects

     let myval = [1,2,3];
     console.log("type:", typeof myval, " value:", myval);
     //result - type: object  value: (3) [1, 2, 3]
    
     myval = { name: "ram"};
     console.log("type:", typeof myval, " value:", myval);
     //result - type: object  value: {name: 'ram'}
    

    One of the weaknesses of typeof is that it does not exactly identify type but generically identifies it as an object, as we can observe in the above code.

  3. typeof with class and function

    
     let myval = function () {}
     console.log("type:", typeof myval, " value:", myval);
     //result - type: function  value: ƒ () {}
    
     myval = class {}
     console.log("type:", typeof myval, " value:", myval);
     //result - type: function  value: class {}
    

    The typeof of a class is a function hence internally the class is a function.

  4. typeof and new keyword

     let myval= new Number(100);
     console.log("type:", typeof myval, " value:", myval);
     // result - type: object  value: Number (100)
    
     myval = new Boolean(false);
     console.log("type:", typeof myval, " value:", myval);
     // result - type: object  value: Boolean (false)
    
     myval = new String("Gopi");
     console.log("type:", typeof myval, " value:", myval);
     // result - type: object  value: String ('Gopi')
    
     myval = new Function();
     console.log("type:", typeof myval, " value:", myval);
     // result - type: object  value: ƒ anonymous() {}
    

    Type of any object (created using new keyword) is always an object.

  5. Miscellaneous scenarios

     let myval= 100
     console.log("type:", typeof myval + 40);
     // result - type: number40
    
     // use parentheses
     console.log("type:", typeof (myval + 40));
     // result - type: number
    
     console.log("type:", typeof typeof (myval));
     // result - type: string
    

Use parentheses in an expression when using with typeof operator.

Complete Source Code

Program 1

// Objective : Understand and apply typeof operator
// Author : Mahavir DS Rathore


let myval=23121212n;
console.log("type:", typeof myval, " value:", myval);
// result - type: bigint  value: 23121212n

myval = undefined;
console.log("type:", typeof myval, " value:", myval);
//result - type: undefined  value: undefined

myval = null;
console.log("type:", typeof myval, " value:", myval);
//result - type: object  value: null

myval = Symbol("Key");
console.log("type:", typeof myval, " value:", myval);
//result - type: symbol  value: Symbol(Key)


myval = function () {}
console.log("type:", typeof myval, " value:", myval);
//result - type: function  value: ƒ () {}

myval = class {}
console.log("type:", typeof myval, " value:", myval);
//result - type: function  value: class {}

myval = [1,2,3];
console.log("type:", typeof myval, " value:", myval);
//result - type: object  value: (3) [1, 2, 3]

myval = { name: "ram"};
console.log("type:", typeof myval, " value:", myval);
//result - type: object  value: {name: 'ram'}

console.log("type:", typeof typeof myval);
//result - type: string

myval= new Number(100);
console.log("type:", typeof myval, " value:", myval);
// result - type: object  value: Number (100)

myval = new Boolean(false);
console.log("type:", typeof myval, " value:", myval);
// result - type: object  value: Boolean (false)

myval = new String("Gopi");
console.log("type:", typeof myval, " value:", myval);
// result - type: object  value: String ('Gopi')

myval = new Function();
console.log("type:", typeof myval, " value:", myval);
// result - type: function  value: ƒ anonymous() {}

 myval= 100
console.log("type:", typeof myval + 40);
// result - type: number40

// use parentheses
console.log("type:", typeof (myval + 40));
// result - type: number

console.log("type:", typeof typeof (myval));
// result - type: string

Video Tutorial

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!