In JavaScript / jQuery, if I alert some object, I get either [object] or [object Object]
Is there any way to know:
-
what is the difference between these two objects
-
what type of Object is this
-
what all properties does this object contains and values of each property
?
You can look up an object’s keys and values by either invoking JavaScript’s native
for inloop:or using jQuery’s
.each()method:With the exception of six primitive types, everything in ECMA-/JavaScript is an object. Arrays; functions; everything is an object. Even most of those primitives are actually also objects with a limited selection of methods. They are cast into objects under the hood, when required. To know the base class name, you may invoke the
Object.prototype.toStringmethod on an object, like this:The above will output
[object Array].There are several other class names, like
[object Object],[object Function],[object Date],[object String],[object Number],[object Array], and[object Regex].