JS: Array literal expression

By Xah Lee. Date: .

Literal expression

// array literal expresion
const x = [3, 4];
console.log(x);
// [ 3, 4 ]

One single trailing comma is ignored

// one trailing comma is ignored
const xx = [3, 4,];
console.log(xx);
// [ 3, 4 ]
console.log(xx.length === 2);
// true

Repeated comma creates sparse array

🛑 warning: Repeated comma creates a Sparse Array.

// repeated comma creates a sparse array.
const x = [9, ,4];

console.log( x );
// [ 9, <1 empty item>, 4 ]

console.log( x.length);
// 3

console.log(
Object.getOwnPropertyNames(x)
); // [ '0', '2', 'length' ]
// note, no property key "1"