How to Push Multiple Values to an Array in Javascript?

Sep 05, 2022 . Admin



Hello dev,

This tutorial shows you how to push multiple values to an array in javascript. We will look at example of how to add multiple values to an array in javascript. you can see push multiple items in array using javascript. I explained simply about insert multiple values into an array in javascript.

In this tutorial, we will explain to you to add multiple items to an array in javascript.to add multiple values, you have to use an array.push() method. e.g. arr. push('b', 'c', 'd');.The push() method adds one or more values to the end of an array.

Second example explains to you we have another method to add multiple values in the array.Use the spread syntax to push multiple values to an array, e.g. arr = [... arrray, 'b', 'c', 'd'];. The spread syntax can be used to unpack the values of the original array followed by one or more additional values.

So, let's see bellow solution:

Example: 1
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Push Multiple Values to an Array in Javascript? - MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Push Multiple Values to an Array in Javascript? -  MyWebtuts.com</h1>
        
        <script type="text/javascript">
            const colors = ["Red","Yellow"];
            colors.push("Green","Pink");

            //give output to console..
            console.log(colors);
        </script>
    </body>
</html>
Output:
['Red', 'Yellow', 'Green', 'Pink']
Example: 2 index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Push Multiple Values to an Array in Javascript? - MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Push Multiple Values to an Array in Javascript? -  MyWebtuts.com</h1>
        
        <script type="text/javascript">
            let fruits = ["Banana","Apple","Kiwi"];
            fruits = [...fruits,"Mango","Graps"];

            //give output to console..
            console.log(fruits);
        </script>
    </body>
</html>
Output:
['Banana', 'Apple', 'Kiwi', 'Mango', 'Graps']
I hope it will help you...
#Javascript