• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Push Multiple Items to Array in JavaScript

Push Multiple Items to Array in JavaScript

August 21, 2022 Leave a Comment

To push multiple items to an array in JavaScript we simply can use the JavaScript Array push() method with the items separated by a commas.

someArray.push("d","e","f");

Let’s show this with a simple example.

var someArray = ["a","b","c"];
someArray.push("d","e",true,1);

console.log(someArray);

#Output
['a', 'b', 'c', 'd', 'e', true, 1]

Push Multiple Items to Array in JavaScript if They Do Not Already Exist in the Array

We can use JavaScript to add multiple items to an array if they do not already exist in the array by making use of the indexOf() and push() methods.

First let’s see how to add a single item to an array if it does not exist in the array.

var someArray = ["a","b","c"];
if( someArray.indexOf("d") == -1 ){
  someArray.push("d");
}

console.log(someArray);

#Output
['a', 'b', 'c', 'd']

In the code above, we simply have an if-else conditional statement that checks whether the item is in the array using the indexOf() method. If the item is not in the array, then the number -1 will be returned. And if that is the case, we can then use the push() method to add the new item to the array.

Since we want to push multiple items to an array, we simply need to add a loop to loop through all the items we want to push. We will remove any items that are already in the array, and then push the items at the end.

var someArray = ["a","b","c"];
var itemsToAdd = ["d","e","a","f"];
for( var i = 0; i<itemsToAdd.length; i++ ){
  if( someArray.indexOf(itemsToAdd[i]) == -1 ){
    someArray.push(itemsToAdd[i]);
  }
}

console.log(someArray);

#Output
['a', 'b', 'c', 'd', 'e', 'f']

Let’s put this code in a function to make things simpler.

function addMultipleItems(arr,items){
  for( var i = 0; i<items.length; i++ ){
    if( someArray.indexOf(items[i]) == -1 ){
      someArray.push(items[i]);
    }
  }
};

In our function above, the user simply needs to give the function the array they want to check, and the items they want to add.

Now let’s test this function out.

function addMultipleItems(arr,items){
  for( var i = 0; i<items.length; i++ ){
    if( someArray.indexOf(items[i]) == -1 ){
      someArray.push(items[i]);
    }
  }
};

var someArray = ["a","b","c","hello",3,false];

addMultipleItems(someArray,["d","3","b","Hello","false"]);

console.log(someArray);

#Output
['a', 'b', 'c', 'hello', 3, false, 'd', '3', 'Hello', 'false']

Remember that with the indexOf() method, characters are case-sensitive. So you will notice that in the example above, “Hello” was added to our array even though “hello” was already in it.

Hopefully this article has been useful in helping you understand how to push multiple items to an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  Add Hours to a Date Using JavaScript
  • 2.  How to Repeat a String in JavaScript
  • 3.  Using JavaScript to Get Substring Between Two Characters
  • 4.  Using JavaScript to Increment a Variable by 1
  • 5.  Using JavaScript to Get the Width of an Element
  • 6.  Using JavaScript to Stop a Timer
  • 7.  JavaScript Print Array – Printing Elements of Array to Console
  • 8.  Reverse For Loop JavaScript
  • 9.  JavaScript Trunc with Math.trunc() Method
  • 10.  Using JavaScript to Get the Max Value in an Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy