JavaScript push() Method: Array Object
Description
The push() method adds one or more elements at the end of an array and returns the new length of the array.
Version
Implemented in JavaScript 1.2
Syntax
push(element1, element2,.................elementN)
Parameters
element1, element2,.................elementN
The elements to add at the end of the array.
Example:
In the following web document, there is an array fruitslist with two elements "Orange" and "Apple". Now push() method adds two elements "Banana", "Chery" at the end of the array.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript push() method example</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : push() method</h1>
<script src="array-push-example1.js"></script>
</body>
</html>
JS Code
var fruitslist = new Array("Orange", "Apple");
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Fruits List : " + fruitslist);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Now add 'Banana' and 'Chery' as new fruits.");
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
var newfruitslist=fruitslist.push("Banana", "Chery");
var newParagraph2 = document.createElement("p");
var newText2 = document.createTextNode("No of Fruits : " + newfruitslist );
newParagraph2.appendChild(newText2);
document.body.appendChild(newParagraph2);
var newParagraph3 = document.createElement("p");
var newText3= document.createTextNode("New fruits List : " + fruitslist);
newParagraph3.appendChild(newText3);
document.body.appendChild(newParagraph3);
View the example in the browser
Practice the example above online
JavaScript push() method exampleSee also:
JavaScript Core objects, methods, properties.
Previous: JavaScript pop() Method: Array Object
Next:
JavaScript reverse() Method: Array Object
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Spread operator
const user = { name: 'Owen', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);
It's possible to combine objects using the spread operator .... It lets you create copies of the key/value pairs of one object, and add them to another object. In this case, we create copies of the user object, and add them to the admin object. The admin object now contains the copied key/value pairs, which results in { admin: true, name: "Owen", age: 21 }.
Ref: https://bit.ly/323Y0P6
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook