Search code examples
javascriptajaxjsonstringify

JSON.stringify() and JavaScript Objects


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.


Solution

  • a = new Object()
    

    and

    a = []
    

    are not equivalent. But,

    a = {}
    

    and

    a = new Object()
    

    are.