var myArray = [1, 2, 3, 4, 5];
push(): Adds an element to the end of an array.
pop(): Removes the last element from an array.
shift(): Removes the first element from an array.
unshift(): Adds an element to the beginning of an array.
splice(): Adds or removes elements from an array.
Inserting an Item in an Array with the splice()
Here’s an example of how to insert an item in an array using the
var myArray = [1, 2, 3, 4, 5]; myArray.splice(2, 0, 6); console.log(myArray); // [1, 2, 6, 3, 4, 5]
In this example, the
splice() method is used to insert the value 6 into the third index (index 2) array
myArray. The first argument of the
splice() method is the index where the item should be inserted, and the second argument is the number of elements to remove. In this case, 0 pieces are removed, as we only want to insert an item into the array.
Arrays are a fundamental concept in, and they provide an easy way to store and manipulate multiple values in a single variable. The
Thanks for reading. Happy coding!