In this article, you will learn about the javascript Array built-in method Array.prototype.push()
. How does this method work in javascript?
The Array.prototype.push()
method adds one or more elements to the end of an array. And return the length of the new array.
array.push(element1, element2, …,elementN);
This method takes no. of N elements as a parameter to be added in an array as given below:
Here are some examples of Array.prototype.push()
method:
//Examples of Array.pop() method
const arr = [40, 90, 160, 250, 360];
//return length
console.log(arr.push(480));
// Output: 6
console.log(arr);
// Original Array After Pushing Element,
// output: [40, 90, 160, 250, 360, 480]
const language = ['Java', 'C#', 'Python', 'SQL Server'];
console.log(language.push("JavaScript", "Go"));
// output: 6
console.log(language);
// Original Array After Pushing Element,
// expected output: ["Java", "C#", "Python", "SQL Server", "JavaScript", "Go"]
//Merging two arrays using the push() method
arr.push(...language);
console.log(arr);
//Output => [40, 90, 160, 250, 360, 480, "Java", "C#", "Python", "SQL Server", "JavaScript", "Go"]
I hope this article will help you to understand the javascript Array built-in method Array.prototype.push().
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments