JavaScript Program To Add Element To Start Of An Array - Programiz
Maybe your like
To understand this example, you should have the knowledge of the following JavaScript programming topics:
- JavaScript Array unshift()
- JavaScript Array splice()
- JavaScript Array concat()
Example 1: Add Element to Array Using unshift()
// program to add element to an array function addElement(arr) { // adding new array element arr.unshift(4); console.log(arr); } const array = [1, 2, 3]; // calling the function // passing array argument addElement(array);Output
[4, 1, 2, 3]In the above program, the new element is added to the array variable using the unshift() method.
The unshift() method adds a new element at the beginning of an array.
Example 2: Add Element to Array Using splice()
// program to add element to an array function addElement(arr) { // adding element to array arr.splice(0, 0, 4); console.log(arr); } const array = [1, 2, 3]; // calling the function addElement(array);Output
[4, 1, 2, 3]In the above program, the splice() method is used to add a new element to an array.
In the splice() method,
- The first argument is the index of an array where you want to add an element.
- The second argument is the number of elements that you want to remove from the index element.
- The third argument is the element that you want to add to the array.
Example 3: Add Element to Array Using Spread Operator
// program to add element to an array function addElement(arr) { // adding element to array arr = [4, ...arr]; console.log(arr); } const array = [1, 2, 3]; // calling the function addElement(arr);Output
[4, 1, 2, 3]In the above program, the spread operator ... is used to add a new element to the beginning of an array.
arr = [4, ...arr]; takes first element as 4 and the rest elements are taken from array.
Example 4: Add Element to Array Using concat()
// program to add element to an array function addElement(arr) { // adding element to array arr = [4].concat(arr); console.log(arr); } const array = [1, 2, 3]; // calling the function addElement(array);Output
[4, 1, 2, 3]In the above program, the concat() method is used to add a new element to an array.
The concat() method combines two arrays into one.
Also Read:
- JavaScript Program to Insert Item in an Array
- JavaScript Program to Append an Object to An Array
Tag » Add Element Array Js
-
How To Append Something To An Array? - Stack Overflow
-
JavaScript Array Push() Method - W3Schools
-
How To Add To An Array With The Push, Unshift, And Concat Functions
-
totype.push() - JavaScript - MDN Web Docs
-
5 Way To Append Item To Array In JavaScript
-
How To Add Elements Into An Array In JavaScript - Linux Hint
-
How To Add New Elements At The Beginning Of An Array In JavaScript
-
JavaScript: How To Insert Elements Into A Specific Index Of An Array
-
Javascript Array Push: How To Add Element In Array - AppDividend
-
JavaScript Program To Insert Item In An Array - Programiz
-
Add An Element To An Array In JavaScript - Mastering JS
-
TypeScript - Array Push() - Tutorialspoint
-
Add Element To Array At Specific Index In JavaScript | Bobbyhadz
-
How To Insert An Element In A Specific Index In JavaScript Array
-
JavaScript Array Splice(): Delete, Insert, And Replace
-
How To Add Item To An Array At A Specific Index In JavaScript
-
Javascript Tutorial | Adding Elements To Array In JavaScript - Morioh
-
Add An Item To The Beginning Of An Array In JavaScript Or Node.js
-
How To Add Element To UseState Array In JavaScript - Simplernerd