'Javascript push() shorthand?

Is there a shorthand for JavaScript's (or even in coffeescript) .push(), when appending a value to an array? Much like php's $array[] = 'added to array';.



Solution 1:[1]

Nope.

You'll just have to use .push().

The use of coffeescript will only afford you the convenience of dropping the parentheses.

Solution 2:[2]

You can use arr[arr.length] = "new value" but there's no other shortcut.

Solution 3:[3]

This is not exactly an array.push() but might come in handy in some situations and works somewhat like it.

const cars = ["Saab", "Volvo", "BMW"];
var myNewArray =  [...cars, "Honda"];

This creates ["Saab", "Volvo", "BMW", "Honda"] and assigns to myNewArray variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joseph Silber
Solution 2 L105
Solution 3 Dula