69

One of my alerts is giving the following result:

[object Object] 

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

bruntime
  • 371
  • 2
  • 13
Sriram
  • 2,769
  • 7
  • 25
  • 35
  • 1
    See also [what does \[object Object\] mean?](http://stackoverflow.com/q/4750225/1048572) – Bergi Nov 04 '14 at 16:16

7 Answers7

63

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.

Matt
  • 72,564
  • 26
  • 147
  • 178
  • 2
    [Read this article for better understanding](https://stackoverflow.com/questions/4750225/what-does-object-object-mean/57310111#57310111) – Himansh Aug 01 '19 at 13:31
15

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(""))

https://jsfiddle.net/b1u6hfns/

ncubica
  • 7,736
  • 7
  • 52
  • 72
9

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.

jetsie
  • 125
  • 1
  • 6
4

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))
Bendy Latortue
  • 421
  • 5
  • 6
2

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.

Wahyu Kristianto
  • 7,521
  • 5
  • 39
  • 64
1

Another option is to use JSON.stringify(obj)

For example:

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

https://www.w3schools.com/js/js_json_stringify.asp

jack1536
  • 111
  • 1
  • 2
0

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)

Guillaume250
  • 454
  • 5
  • 5