Though the ‘push’ method is sufficient in most cases, the limitation with this method can be, the element will be added only at the end of the array.
Add an element at the beginning of an Array
For some odd reason, if you do not want to use ‘push’ method, but want to append an element at the end, we can use the ‘splice’ method.
Adding an Element to an Array using ‘concat’ method
Yes, there is yet another way to do it.
We can also use the newly available spread operator (…, three dots) to append elements to an array. Here is the code
In the above example, not just one (in this case 4), you can add as many elements as you want separated by commas.
We can use the ‘splice’ method to add an element at any specific position in an array. Earlier, we have seen how to add en element at the end using the ‘splice’ method, similarly, we can use it to add an element at any specific position.
In the above example, we inserted a value 2 at position 1.
Leave A Comment