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:
For the push() method, use the given syntax:
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:
Create variable “element” to store a value “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”:
Now, in the conditional statement, check if the element “java” does not exist in the array, then push it by calling the “push()” method:
array.push(element);
}
Finally, print the array on the console:
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:
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:
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:
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.