Commas in arrays

The specification of Javascript allows for elision - the omission of element values - when initializing an array. While this behavior has a history of browser-specific bugs, it is a useful way to increase array length when used carefully.

Loading TypeScript...

The empty space creates a missing element. Notice that an empty space is not created for a trailing comma.

Loading TypeScript...

The missing element is not store a null or undefined value - it simply isn't there.

Loading TypeScript...

The missing space is referred to as a hole. A hole is equivalent to undefined in comparison, not null.

Loading TypeScript...

The in operator will report false when checking for the index of a hole. In this way, a hole is functionally similar to an element that is out of bounds.

Loading TypeScript...

Even assigning the value undefined to the array will fill the hole.

Loading TypeScript...

Was this page helpful?