What does [] mean in JavaScript?

it is an array literal. It is not quite the same as declaring new Array() – the Array object can be overwritten in JavaScript, but the array literal can’t. Here’s an example to demonstrate

// let's overwrite the Array object
Array = function(id) {
 this.id = id;
}

var a = new Array(1);
var b = [];

console.log(a.hasOwnProperty("id")); // true
console.log(b.hasOwnProperty("id")); // false

console.log(a.push); // false, push doesn't exist on a
console.log(b.push); // true,  but it does on b

b.push(2);
console.log(b); // outputs [2]

Leave a Comment