javascriptobject-model

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


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"}

Solution

  • 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.