Javascript {} []

Someone asked me what is the difference between {} and [] within javascript.

Basically the difference is one is a key => value pair like PHP ‘{}’, and the other is just a pure array of values ‘[]’.

Here is the code.

var simplearray = ["genux","linux"];
 
var keyvaluearray = {"name" : "genux", "OS" : "linux"};

To access the different values, since the simple array is index by numbers 0 onwards you use the [x] notation, and the key value array you can reference the value by the name of the key like in object orientated way.

alert(simplearray[0]);
 
alert(keyvaluearray.name);

Leave a Reply

Your email address will not be published.