- Javascript: check if an array includes a value (6 ways)
- Check if a value exists in javascript array using includes()
- Frequently Asked:
- 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
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- Javascript check if value exists in array
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Check if value exists in Array – jQuery and JavaScript
- Contents
- 1. Using Loop to check value exists in Array
- 2. Array. indexOf()
- 3. jQuery.inArray()
- 4. Conclusion
Javascript: check if an array includes a value (6 ways)
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()
Javascript’s includes() method finds out if a particular value exists in the array or not .
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
Frequently Asked:
function existsInArray(_element,_array) < console.log(_array.includes(_element)); >//usage let myArray = ["Javascript", "Is", "Popular","Language"]; existsInArray("Popular",myArray); existsInArray("Hello",myArray);
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.
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
function existsInArray(_element,_array) < console.log(_array.indexOf(_element) >=0 ); > //usage let myArray = ["Javascript", "Is", "Popular","Language"]; existsInArray("Popular",myArray); existsInArray("Hello",myArray);
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
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
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));
Explanation:-
- In the above code, function existsInArray(_element,_array) is used.
- Within the function, for loopruns, 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 .
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
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));
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() metho d returns a new array that consists of all the elements that pass the test implemented by the function provided.
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
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));
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 thefirst 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 ena bles 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.
Check if the value s ‘Popular’ and ‘Hello’ exist in the array [“Javascript”, “Is”, “Popular”,”Language”]
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));
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.
I hope this article helped you to check if a particular value exists in the javascript array. Good Luck .
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Javascript check if value exists in array
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Check if value exists in Array – jQuery and JavaScript
In this tutorial, I show how you can check whether an Array already contains a specific value or not using Javascript and jQuery.
- Stop new value from insert if it already exists in an Array.
- Execute script when the Array contains a particular value.
- etc.
If you are familiar with PHP, where you can use the in_array() function to search value in the Array, and it returns the Boolean value ( TRUE or FALSE ).
There are inbuilt methods in jQuery and JavaScript that return the index position of the value which you can use for the search.
Contents
1. Using Loop to check value exists in Array
You can easily find the value within an Array by traversing the Array and check for the value.
status : Exist status : Not exist
For making your searching process simpler you can use jQuery and JavaScript inbuilt function.
2. Array. indexOf()
It is a JavaScript method that returns the index position of the value. If it doesn’t find the value then it returns -1.
It works both with string and an Array.
value-from-which-search.indexOf( search-value );
Completed Code
Index : 2 Index : -1 Index : 3
3. jQuery.inArray()
jQuery inArray function returns the index position of the value when it finds the value otherwise returns -1.
It also works with string and an Array.
jQuery.inArray( search-value, value-from-which-search);
Index : 2 Index : -1 Index : 3
4. Conclusion
I discussed some of the methods which you can use to search your value in the existing Array. Using this you can remove or replace the duplicate values.
If you are using jQuery within your project then you can simply go with inArray() method otherwise you can indexOf() method.
If you found this tutorial helpful then don’t forget to share.