While working in javascript arrays, there is often a requirement to determine if a value exists in an array. This article demonstrates easy ways to check if an array includes a particular value in a javascript using different methods and example illustrations.
Table of Contents:
- Check if a value exists in javascript array using includes()
- Check if a value exists in javascript array using indexOf()
- Check if a value exists in javascript array using iteration
- Check if a value exists in javascript array using some()
- Check if a value exists in javascript array using filter()
- Check if a value exists in javascript array using Set
Check if a value exists in javascript array using includes()
Javascript’s includes() method finds out if a particular value exists in the array or not.
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Frequently Asked:
Code:-
function existsInArray(_element,_array) { console.log(_array.includes(_element)); } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; existsInArray("Popular",myArray); existsInArray("Hello",myArray);
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, includes() method is used to check if the _element (first argument) exists in the _array (second argument).
Check if a value exists in javascript array using indexOf()
Javascript’s indexOf() method returns the first index of the element if found in the array. If the element is not found then, -1 is returned.
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Code:-
function existsInArray(_element,_array) { console.log(_array.indexOf(_element) >=0 ); } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; existsInArray("Popular",myArray); existsInArray("Hello",myArray);
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, indexOf() method is used to check if the _element (first argument) exists in the _array (second argument). If the value is not found then _array.indexOf(_element) is less than zero. Else _array.indexOf(_element) is -1.
Check if a value exists in javascript array using iteration
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Code:-
function existsInArray(_element,_array) { var length = _array.length; for(var i = 0 ; i < length; i++) { if(_array[i] == _element) { return true; } } return false; } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; console.log(existsInArray("Popular",myArray)); console.log(existsInArray("Hello",myArray));
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, for loop runs, iterates through all the array elements and compares each element with _element (first argument). If the match is found, true is returned. Else false is returned.
Check if a value exists in javascript array using some()
Javascript’s some() method will test a callback function on all the elements of the calling array and returns true if it finds the element for which the callback function returns true.
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Code:-
function existsInArray(_element,_array) { return _array.some(function(element) { return element == _element; } ) } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; console.log(existsInArray("Popular",myArray)); console.log(existsInArray("Hello",myArray));
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Here, some() method applies a function on all array elements to find if any element is equal to the value passed in as an argument(_element). If any of the element is found, true is returned. Else, false is returned.
Check if a value exists in javascript array using filter()
Javascript’s filter() method returns a new array that consists of all the elements that pass the test implemented by the function provided.
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Code:-
function existsInArray(_element,_array) { return _array.filter(function(e) { return e == _element }).length > 0 } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; console.log(existsInArray("Popular",myArray)); console.log(existsInArray("Hello",myArray));
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, filter() is applied, which implements a function on all the array elements and creates a new array with elements that have a value equal to the _element passed in as the first argument.
- Finally, check if the length of the filtered array is greater than zero. If yes, return true, else return false.
Check if a value exists in javascript array using Set
Javascript’s Set enables saving unique values.
Javascript’s Set.prototype.has() method is used to find out if an element with a particular value exists in the SET or not.
Example:-
Check if the values ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Code:-
function existsInArray(_element,_array) { var setOfArrayElements = new Set(_array); if (setOfArrayElements.has(_element)) return true; else return false; } //usage let myArray = ["Javascript", "Is", "Popular","Language"]; console.log(existsInArray("Popular",myArray)); console.log(existsInArray("Hello",myArray));
Output:-
true false
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, all the elements of the array are stored in Set.
- Finally, has() method of Set is used to find if the Set (setOfArrayElements) contains an element with a particular value.
Read More:
- Javascript: Insert an item to array at specific index
- Javascript: Check if an array is empty
- Javascript: Convert array to string (4 ways)
I hope this article helped you to check if a particular value exists in the javascript array. Good Luck !!!