Dev Corner

Software Developer’s Notepad

Sometimes we should find what the is the type of a given variable. In JavaScript there is a simple way to solve this problem - using the operator typeof. This operator returns as result of the check a string, which could have one of the listed values:

  • number
  • string
  • boolean
  • object
  • function
  • undefined

Example #1

typeof(123);			//This will return number
typeof("abd");			//This will return string
typeof(false);			//This will return boolean
 
 
typeof(typeof(var));		
//This will return string - no matter what the type of var is

Sometimes it is possible that IE thinks that a function is actually an object.

Example #2

typeof(document.getElementById);			
//The possible result for this operation is object, instead of function

Add A Comment

You must be logged in to post a comment.