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: Operand is an expression representing the object or primitive whose type is to be returned. The possible types that exist in javascript are:
Below is an example of the typeof operator.
Example: This example checks the typeof of a string, number, and undefined object and returns the value in the console.
javascript
console.log( typeof 'mukul' )
console.log( typeof 25)
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: Typeof Number, in this sample, we used ‘===’ (strict equality comparison operator) which compare value and type both and then return true or false. For example- consider the first console.log(), the js starts compiling from left to right and it first calculates the type of 25 which is ‘number’, and then compares it with ‘number’ and then finally returns true or false accordingly.
javascript
console.log( typeof 25 === 'number' );
console.log( typeof 3.14 === 'number' );
console.log( typeof (69) === 'number' );
console.log( typeof Math.LN10 === 'number' );
console.log( typeof Infinity === 'number' );
console.log( typeof NaN === 'number' );
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
console.log( typeof '' === 'string' );
console.log( typeof 'bla' === 'string' );
console.log( typeof `template literal` === 'string' );
console.log( typeof '1' === 'string' );
console.log( typeof ( typeof 1) === 'string' );
console.log( typeof String(1) === 'string' );
|
Output:
true
true
true
true
true
true
Example: Typeof boolean
javascript
console.log( typeof true === 'boolean' );
console.log( typeof false === 'boolean' );
console.log( typeof !!(1) === 'boolean' );
|
Output:
true
true
true
Example: Typeof undefined
javascript
console.log( typeof undefined === 'undefined' );
console.log( typeof variable === 'undefined' );
|
Output:
true
true
Example: Typeof symbol
javascript
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
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
console.log( typeof function () { } === 'function' );
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,
GeeksforGeeks 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!
Last Updated :
19 Jun, 2023
Like Article
Save Article