Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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';.

like image 760
Shaz Amjad Avatar asked Sep 16 '13 01:09

Shaz Amjad


People also ask

What is push () JS?

JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array.

What can I use instead of push in JavaScript?

Alternatives of push() method in JavaScript Approach 1: Use the length property to insert the element at the end of the array. Example: This example implements the above approach.

What is the opposite of push () in JavaScript?

The opposite of push() (as the question is titled) is pop() .

How do you push a number in JavaScript?

The arr. push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.


2 Answers

Nope.

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

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

like image 140
Joseph Silber Avatar answered Nov 03 '22 21:11

Joseph Silber


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

like image 36
L105 Avatar answered Nov 03 '22 23:11

L105