In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable. 

Syntax:

typeof operand

OR

typeof (operand)

Note: An operand is an expression that represents the object or primitive whose type is returned. The types that can exist in JavaScript are:

  • undefined
  • Object
  • boolean
  • number
  • string
  • symbol
  • function

Below is an example of the typeof operator.

Example: This example checks for undefined string, number, and object type and returns the value to the console.

JavaScript

// "string"
console.log(typeof 'mukul')
// "number"
console.log(typeof 25)
// "undefined"
console.log(typeof variable)

Output:

string
number
undefined

Let’s cover all the types one by one dedicating a single code section for each code.

Example: Number Type, in this example we used “===” (strict equality comparison operator) to compare value and type, then return true or false. For example, consider first console.log(), js starts compiling from left to right, and first it calculates type 25 as “number”, then compares it with “number”, then finally both return true or false respectively.

JavaScript

//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');

Output:

true
true
true
true
true
true
true

Fun fact NaN which stands for not-a-number has a type of “number”.

Example: Typeof string

JavaScript

// string
console.log(typeof '' === 'string');
console.log(typeof 'bla' === 'string');
// ES6 template literal
console.log(typeof `template literal` === 'string');
console.log(typeof '1' === 'string');
console.log(typeof (typeof 1) === 'string');
// Wrapping inside String() function
console.log(typeof String(1) === 'string');

Output: 

true
true
true
true
true
true

Example: Typeof boolean

JavaScript

// Boolean
console.log(typeof true === 'boolean');
console.log(typeof false === 'boolean');
// Two calls of the ! (logical NOT) operator
// are equivalent to Boolean()
console.log(typeof !!(1) === 'boolean');

Output: 

true
true
true

Example: Typeof undefined

JavaScript

// Undefined
console.log(typeof undefined === 'undefined');
// Declared but undefined variable
console.log(typeof variable === 'undefined');

Output: 

true
true

Example: Typeof symbol

JavaScript

// Symbol
console.log(typeof Symbol() === 'symbol');
console.log(typeof Symbol('party') === 'symbol');
console.log(typeof Symbol.iterator === 'symbol');

Output: 

true
true
true

Example: Typeof object

JavaScript

// Object
console.log(typeof { b: 1 } === 'object');
console.log(typeof [1, 2, 9] === 'object');
console.log(typeof new Date() === 'object');

Output: 

true
true
true

Example: Typeof function

JavaScript

// 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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, codeconfig.in Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then