• 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
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Add Item to Array if it Does Not Exist

Using JavaScript to Add Item to Array if it Does Not Exist

July 6, 2022 Leave a Comment

We can use JavaScript to add an item to an array if it does not already exist in the array by making use of the indexOf() and push() methods.

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.

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

function addItem(arr,item){
  if( arr.indexOf(item) == -1 ){
    someArray.push(item);
  }
};

In our function above, the user simply needs to give the function the array they want to check, and the item they want to add if it does not exist.

Now let’s test this function out.

function addItem(arr,item){
  if( arr.indexOf(item) == -1 ){
    someArray.push(item);
  }
};

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

addItem(someArray,"d");
addItem(someArray,"3");
addItem(someArray,"b");
addItem(someArray,"Hello");
addItem(someArray,"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.

Adding Items From One Array to Another if They Do Not Exist

In this example, we will simply have an array of colors that we want to add to another array of colors, and only add the color if it does not exist. To do this we can use our code from above and simply add a for loop.

Here is the JavaScript code to do this:


var array1 = ["black","red","blue","green","yellow","pink","orange","brown","white","purple"];
var array2 = ["blue","green","yellow","red","teal"];
for( var i = 0; i<array1.length; i++ ){
  if( array2.indexOf(array1[i]) == -1 ){
    array2.push(array1[i]);
  }
};

console.log(array2);

#Output
['blue', 'green', 'yellow', 'red', 'teal', 'black', 'pink', 'orange', 'brown', 'white', 'purple']

Hopefully this article has been useful in helping you understand how to use JavaScript to add an item to an array if the item not exist.

Other Articles You'll Also Like:

  • 1.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 2.  Using JavaScript to Capitalize the First Letter of a String
  • 3.  JavaScript Change Background Color of Element
  • 4.  Using JavaScript to Get the Current URL
  • 5.  JavaScript Check if Attribute Exists in HTML Element
  • 6.  Using JavaScript to Get the Min Value in an Array
  • 7.  How to Clear an Input Field in JavaScript
  • 8.  Using JavaScript to Scroll to Bottom of Div
  • 9.  Using JavaScript to Run a Function Every 5 seconds
  • 10.  JavaScript asin – Find Arcsine and Inverse Sine of Number

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy