JavaScript – Add an Element at the End of Array
To add and element at the end of array in JavaScript, call push() method on this array and pass the element as argument.
The syntax to add/append an element e
to the end of an array arr
is
</>
Copy
arr.push(e)
Example
In the following example, we create an array with three elements, and add an element 'orange'
to the end of this array.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var arr = ['apple', 'banana', 'cherry'];
arr.push('orange');
document.getElementById('output').innerHTML = 'Array : ' + arr;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to append/add an element to the end of an Array in JavaScript, with examples.