JavaScript

Push Element in an Array if it Does not Exist Using JavaScript

Sometimes, arrays and other data structures store duplicate values. So, to avoid duplication, programmers try to verify whether the specified element exists in the array or not and then insert an element in the array if the particular element does not exist. For this purpose, JavaScript provides some pre-built methods, such as includes() and indexOf() methods.

This post will describe the methods to push the element if it does not exist in the array in JavaScript.

How to Push Element in an Array if it Does not Exist/Occur Using JavaScript?

If the element does not exist in an array, push them into an array using the following methods:

Let’s examine the working of these methods one by one!

Method 1: Push Element in an Array if it Does not Exist Using includes() Method With push() Method

Use the “includes()” method with the “push()” method to check if the specific element exists in the array or not. If the element does not occur, push it into the array. The includes() method gives a boolean value “true” when the element exists in the array else it gives “false”.

Syntax

Use the given syntax for the includes() method:

array.includes(element)

For the push() method, use the given syntax:

array.push(element)

In the above syntax, the “element” is an argument that needs to be checked in an array, whether it exists or not; if it doesn’t, then push it into an array.

Example

In the following example, first, create an array of programming languages:

const array = ["HTML", "CSS", "JavaScript", "Java"];

Create variable “element” to store a value “java”:

const element = "java";

Call the includes() method and pass the value as an argument. If the “java” exists in the array, it returns “true” and stores it in a variable “elementExists”:

const elementExists = array.includes(element);

Now, in the conditional statement, check if the element “java” does not exist in the array, then push it by calling the “push()” method:

if (!elementExists) {

array.push(element);

}

Finally, print the array on the console:

console.log(array);

As you know, JavaScript is a case-sensitive scripting language, so “Java” and “java” are not equal. As a result, the “includes()” method gives “false()” and the “push()” method pushes it in an array:

If the variable stores “Java”, the includes() method gives “true” because it already exists in the array and nothing will be pushed in the array:

const element = "Java";

Output

Method 2: Push Element in an Array if it Does not Exist Using indexOf() Method With push() Method

Another method to verify and push the element in an array is the “indexOf()” method with the “push()” method. The indexOf() method gives “-1” as an output if the provided element does not occur in the array.

Syntax

Follow the given syntax for the indexOf() method:

array.indexOf(element)

Example

Here, we will check if the value of the “array.indexOf(element)” is equivalent to the “-1”; it will push the element in an array:

if (array.indexOf(element) === -1) {

array.push(element);

}

As the “Java” element is already present in the array, the “indexOf()” method gives “1” which doesn’t satisfy the added condition, so, nothing will be added to the array:

We have compiled all the essential instructions related to pushing the element if it does not exist in an array in JavaScript.

Conclusion

To verify if the provided element exists in an array or not, use the “includes()” and “indexOf()” methods, and if it is not present in the array, then push it into an array using the “push()” method. The includes() method returns “true” if an element exists; else, it returns “false” while the indexOf() method gives “1” when the element is present else, its outputs “-1”. In this post, we described the methods to push the element if it does not exist in the array in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.