how to write an array literal notation in JS?
var myArray = new Array();
myArray.prop = 'test';
i've already tried this, but it logs undefined value.
var myArray = [prop='test'];
console.log(myArray.prop);
how to write an array literal notation in JS?
var myArray = new Array();
myArray.prop = 'test';
i've already tried this, but it logs undefined value.
var myArray = [prop='test'];
console.log(myArray.prop);
The array literal is:
['foo', 'bar', 'baz']
You cannot use keys in this, because Javascript arrays do not have string keys. They're numerically indexed lists. Setting a property on an array object does not do what you think it does.
What you're looking for is an object literal:
{ foo : 'bar', baz : 42 }
Arrays in JavaScript cannot have string indices... they must be numbers. What you're looking for in your case is an object.
var myObject = {
prop: 'test'
}