Everything is not as simple as described in the answers above ... they are usually not in javascriptland;)
typeof is the "official" function that is used to get type in javascript, however in some cases this may lead to unexpected results ...
1. Lines
typeof "String" or
typeof Date(2011,01,01)
"line"
2. Numbers
typeof 42 or
typeof NaN , lol
"number"
3. Bool
typeof true (valid values ββare true and false )
"logical"
4. Object
typeof {} or
typeof [] or
typeof null or
typeof /aaa/ or
typeof Error()
"an object"
5. Function
typeof function(){}
"function"
6. Undefined
var var1; typeof var1
"undefined"
An alternative is to use ({}).toString() , which will get you a slightly more accurate answer most of the time ...
source share