How to use push() Method In Javascript
The JavaScript Array push() Method is used to add one or more values to the end of the array. This method changes the length of the array by the number of elements added to the array.
Syntax:
arr.push(element0, element1, … , elementN)
Example: In this example, we are using the push() method to add a new element in the last index of the array.
const num1 = [1, 2, 3];
num1.push(4);
console.log(num1);
// adding multiple element i the last index of array
const num2 = [10, 20, 30, 40, 50];
num2.push(60, 70);
console.log(num2);
Output
[ 1, 2, 3, 4 ] [ 10, 20, 30, 40, 50, 60, 70 ]
How to Add Elements to the End of an Array in JavaScript ?
Appending elements to the end of an array is a fundamental task in JavaScript, essential for data manipulation and management. This guide will provide an overview of various methods to achieve this, enhancing your coding efficiency.