How to Add Element at the End of Array in JavaScript?

Sep 02, 2022 . Admin



Hello dev,

This post will give you example of how to add an element at end of an array in javascript. I explained simply about javascript add an element at end of an array. We will use how to add an element in the array at the end using javascript.

in this tutorial, we will explain to you to add a new element at end of the array in javascript.to add a new element at the end you have to use the length property of the array. To add the item to the array without push call: arr[arr. length] = value;

So, let's see bellow solution:

Example:
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Add Element at End of Array in JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Add Element at End of Array in JavaScript? -  MyWebtuts.com</h1>
        
        <script type="text/javascript">
            const colors = ["Red","Yellow","Green"];
            colors[colors.length] = "White";

            // give output to console..
            console.log(colors);
        </script>
    </body>
</html>
Output:
['Red', 'Yellow', 'Green', 'White']
I hope it will help you...
#Javascript