How to determine if a javascript object is simple or complex?

Andrey picture Andrey · Sep 20, 2011 · Viewed 7.2k times · Source

Basically I need to tell apart the following two:

var simple = 5 // or "word", or 56.78, or any other "simple" object
var complex = {propname: "propvalue", "otherprop": "othervalue"}

Answer

John Hartsock picture John Hartsock · Sep 20, 2011

Using typeof operator you can determine the following:

"number"        Operand is a number
"string"        Operand is a string
"boolean"       Operand is a Boolean
"object"        Operand is an object
"undefined"     Operand is not defined.

Edited: As it was suggested in a comment you may want to also check if value is null, as typeof null will return object.