JSON.stringify() and JavaScript Objects

BuddyJoe picture BuddyJoe · May 16, 2011 · Viewed 18.6k times · Source

I'm thinking maybe I missed something in JavaScript that I'm just picking up now.

I tried this code in Chrome console:

a = [];
a.name = "test";
JSON.stringify(a); 
// which returns value []
a = new Object();
a.name = "test";
JSON.stringify(a); 
// which returns value {"name":"test"}

What is the difference? I thought new Object() was a Microsoft JScript thing? What am I missing? Must have missed something in a spec somewhere. Thanks.

Answer

Anurag picture Anurag · May 16, 2011
a = new Object()

and

a = []

are not equivalent. But,

a = {}

and

a = new Object()

are.