What is Javascript typeof operator?

Consider you have created a page, in which you are taking input from the user and then providing output depending upon that input. Now for checking what type of data is entered (a number or text/string etc.) by the user. So, for this purpose typeof will be used.

JavaScript typeof Operator
JavaScript typeof Operator

Javascript typeof() operator

This typeof operator is used to identify the data type of a variable (in which a value is stored). The data type can be a number, string, object or boolean, etc.


Since typeof is an operator, it has a single operand. To find the data type of the variable, the name of the variable name is written in its operand. The result of this operator is in the form of text or string like β€˜number’, β€˜string’ etc. It can be written as:

  • Method#01: Simply write operation and operand with a space in between the typeof and operand as,
typeof operand
  • Method#02: Write the operation like a function and operand as its argument/ parameter as shown:


Now, let’s elaborate on the different syntax of typeof operator with the help of coding examples.

Example code using Method#01

var arg = 5
document.write(typeof arg)
var arg= 5.5
document.write(typeof arg)


In javascript both integers and float values have data type β€˜numbers’.

Example code using Method#02:

var arg = "AlgoIdeas"
document.write( typeof(arg))


The typeof Operator


typeof 10 // Output will be "number"
typeof 10.14 // Output will be "number"
typeof NaN // Output will be "number"


typeof "AlgoIdeas"  // "string"
typeof "+"	 // "string" (all operators e.g +, -, *, /)
typeof " " // "string" (empty is also a string)


typeof false // "boolean"
typeof true // "boolean"


typeof function () {} // "function"
typeof function myFunc(){} // "function"


typeof [0,1,2,3,4,5]   // "object"
typeof {firstName:"Algo", lastName:"Ideas"}   // "object"
typeof new Date()   // "object"
typeof null   // "object"


let x;
console.log(typeof x);  // "undefined" (as x has no value)

Object is the data type for complex data structures as mentioned above.

Test the data type

As we know that typeof gives the result in the form of text that is a string so we can easily check the data type by using the strict equality comparison operator (===) that will compare the value and our written type for comparison and will show the result as true or false.

console.log(typeof 999 === 'number');  
console.log(typeof "AlgoIdeas" === 'string');

  • This shows that it is true that 999 is a number and "AlgoIdeas" is a string.


JavaScript typeof operator & parenthesis

Although we can use typeof operator both with or without parenthesis. But in some cases, parenthesis plays important for in the results. Let us see the behavior of the operator typeof with respect to parenthesis.

console.log(typeof (999 + '10'))	// with parenthesis
console.log(typeof 999 + '10')		// without parenthesis
  • Line#1: We can see that 999 is a number and 10 is a string here. But in this case, the final result obtained typeof is string.
  • Line#2: Here typeof operator considered only 999 as an operand and given result number 10 is attached because β€˜10’ is a string and the result 'number' is also a string, and due to '+' plus operator both the strings are concatenated and we obtained the final result as number10.


Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...