How to use array.splice method In Javascript
In this method, we will use an array.splice method. The array.splice() method is used to modify the content of the array.
Example: The below example explians the use of the splice() method to add element at start of the array.
let Arr = [1, 2, 3, 4, 5];
console.log(`Array Elements Before
element addition:
${Arr}`);
function myGFG() {
Arr.splice(0, 0, "6");
console.log(`Array Elements After
element addition:
${Arr}`);
}
myGFG();
Output
Array Elements Before element addition: 1,2,3,4,5 Array Elements After element addition: 6,1,2,3,4,5
Add new elements at the beginning of an array using JavaScript
In this article, we will learn how to add new elements at the beginning of an array using JavaScript. We can do this by using the following methods:
Table of Content
- Using the Array unshift() method
- Using array.splice method
- Using the Spread operator
- Using Array concat() Method
- Using array destructuring