• 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 / 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.  Using JavaScript to Get the Scroll Position
  • 2.  How to Empty a String in JavaScript
  • 3.  How to Use JavaScript to Change Button Text
  • 4.  Using JavaScript to Hide Element by Class
  • 5.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 6.  Using JavaScript to Add Trailing Zeros
  • 7.  JavaScript String startsWith Method
  • 8.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 9.  Remove Braces from String Using JavaScript
  • 10.  How to Repeat Character N Times in JavaScript

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