One of my alerts is giving the following result:

[object Object] 

What does this mean exactly? (This was an alert of some jQuery object.)

1

7 Answers

It means you are alerting an instance of an object. When alerting the object, toString() is called on the object, and the default implementation returns [object Object].

var objA = {}; var objB = new Object; var objC = {}; objC.toString = function () { return "objC" }; alert(objA); // [object Object] alert(objB); // [object Object] alert(objC); // objC 

If you want to inspect the object, you should either console.log it, JSON.stringify() it, or enumerate over it's properties and inspect them individually using for in.

1

As @Matt answered the reason of [object object], I will expand on how to inspect the value of the object. There are three options on top of my mind:

  • JSON.stringify(JSONobject)
  • console.log(JSONobject)
  • or iterate over the object

Basic example.

var jsonObj={ property1 : "one", property2 : "two", property3 : "three", property4 : "fourth", }; var strBuilder = []; for(key in jsonObj) { if (jsonObj.hasOwnProperty(key)) { strBuilder.push("Key is " + key + ", value is " + jsonObj[key] + "\n"); } } alert(strBuilder.join("")); // or console.log(strBuilder.join("")) 

The alert() function can't output an object in a read-friendly manner. Try using console.log(object) instead, and fire up your browser's console to debug.

1

That is because there are different types of objects in Javascript!

For example

  • Function objects:

stringify(function (){}) -> [object Function]

  • Array objects:

stringify([]) -> [object Array]

  • RegExp objects

stringify(/x/) -> [object RegExp]

  • Date objects

stringify(new Date) -> [object Date]

... 
  • Object objects!

stringify({}) -> [object Object]

the constructor function is called Object (with a capital "O"), and the term "object" (with small "o") refers to the structural nature of the thingy.

When you're talking about "objects" in Javascript, you actually mean "Object objects", and not the other types.

If you want to see value inside "[Object objects]" use:

console.log(JSON.stringify(result)) 

If you are popping it in the DOM then try wrapping it in

<pre> <code>{JSON.stringify(REPLACE_WITH_OBJECT, null, 4)}</code> </pre> 

makes a little easier to visually parse.

0

Another option is to use JSON.stringify(obj)

For example:

exampleObj = {'a':1,'b':2,'c':3}; alert(JSON.stringify(exampleObj)) 

Alerts aren't the best for displaying objects. Try console.log? If you still see Object Object in the console, use JSON.parse like this > var obj = JSON.parse(yourObject); console.log(obj)

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy