JavaScript | typeof operator

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 exists in javascript are:

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

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example: Typeof string

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



Examople: Typeof boolean

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example: Typeof undefined

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example: Typeof symbol

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example: Typeof object

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example: Typeof function

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
  // function
  console.log(typeof function() {} === 'function');
  
  //classes too are objects
  console.log(typeof class C {} === 'function'); 
  console.log(typeof Math.sin === 'function'); sin function(maths)
</script> 

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : immukul