Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add an element in the last of a JavaScript array?
In this tutorial, we will learn how to add an element to the last of a JavaScript array. The most common method to add an element to the end of the array is by using the Array push() method, but we will discuss multiple methods to achieve this.
Using the Array.prototype.push() Method
Using the Array.prototype.splice() Method
Using Array Indexing
Using the Array.prototype.push() Method
To add an element at the end, use the JavaScript push() method. The push() method appends the given element(s) to the end of the array and returns the new length of the array.
Syntax
arr.push(element1, element2, ..., elementN)
Here arr is the original array to which elements are to be added. The push() method modifies the original array and returns its new length.
Example
<html>
<head>
<title>Program to add an element at the end of a JavaScript array</title>
</head>
<body>
<h3>Adding elements using Array.push() Method</h3>
</body>
<script>
// Create an array named arr
let arr = [1, 2, 3, 4, 5];
// Add elements to the array
arr.push(6);
arr.push(7);
arr.push(8, 9, 10); // Adding multiple elements
// Print the array
document.write("Array after push(): " + arr);
</script>
</html>
Array after push(): 1,2,3,4,5,6,7,8,9,10
Using the Array.prototype.splice() Method
JavaScript array splice() method changes the content of an array by removing existing elements and/or adding new elements. To add elements at the end, we use the array's length as the starting index.
Syntax
array.splice(index, deleteCount, element1, element2, ..., elementN)
Parameters
index ? The index at which to start changing the array
deleteCount ? The number of elements to remove (use 0 for adding only)
element1, ..., elementN ? Elements to add to the array
Example
<html>
<head>
<title>Program to add elements using splice()</title>
</head>
<body>
<h3>Adding elements using Array.splice() Method</h3>
</body>
<script>
// Create an array named arr
let arr = [1, 2, 3, 4, 5];
// Add single element at the end
arr.splice(arr.length, 0, 6);
// Add multiple elements at the end
arr.splice(arr.length, 0, "7th element", "8th element", "9th element");
// Print the array
document.write("Array after splice(): " + arr);
</script>
</html>
Array after splice(): 1,2,3,4,5,6,7th element,8th element,9th element
Using Array Indexing
Since array indexing starts at 0, the next available index for a new element is equal to the array's length. We can directly assign a value to this index to add an element at the end.
Syntax
arr[arr.length] = element
Example
<html>
<head>
<title>Program to add elements using indexing</title>
</head>
<body>
<h3>Adding elements using array indexing</h3>
</body>
<script>
// Create an array named arr
let arr = [1, 2, 3, 4, 5];
// Add elements using indexing
arr[arr.length] = 6;
arr[arr.length] = 7;
arr[arr.length] = 8;
// Print the array
document.write("Array after indexing: " + arr);
</script>
</html>
Array after indexing: 1,2,3,4,5,6,7,8
Comparison
| Method | Performance | Multiple Elements | Returns Value |
|---|---|---|---|
push() |
Fastest | Yes | New array length |
splice() |
Slower | Yes | Array of removed elements |
| Array indexing | Fast | One at a time | The assigned value |
Conclusion
The push() method is the most common and efficient way to add elements to the end of an array. Use splice() when you need more control over array modification, and array indexing for simple single-element additions.
