Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
like image 444
Andrey Avatar asked Sep 19 '11 22:09

Andrey


People also ask

What is simple object in JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

How do you check if an object is a specific type JavaScript?

The JavaScript instanceof operator is used to check the type of an object at the run time. It returns a boolean value(true or false). If the returned value is true, then it indicates that the object is an instance of a particular class and if the returned value is false then it is not.

What is object type in JavaScript?

An object type is simply a collection of properties in the form of name and value pairs. Notice from the list that null and undefined are primitive JavaScript data types, each being a data type containing just one value.


1 Answers

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.

like image 123
John Hartsock Avatar answered Nov 03 '22 00:11

John Hartsock