How do I check if an array includes a value in JavaScript

0 votes

What is the shortest and most efficient technique to determine whether a JavaScript array has a value?

This is the only method I know:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

Is there a more efficient and succinct method to achieve this?

Sep 28 in Java by Nicholas
• 7,420 points
53 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,890 points
310 views
0 votes
4 answers

How do I check if a file exists in Java?

To test to see if a file ...READ MORE

answered Dec 29, 2020 in Java by Rajiv
• 8,910 points
10,962 views
0 votes
1 answer

How do I check if a string contains a substring in Java?

Hi@akhtar, The first and foremost way to check ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,380 points
5,495 views
0 votes
0 answers

Check if a value is an object in JavaScript

How do you check if a value ...READ MORE

1 day ago in Java by Nicholas
• 7,420 points
4 views
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,190 points
2,592 views
0 votes
2 answers

How do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9 in Java by Soham
• 9,670 points
766 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,280 points
20,322 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,800 points
5,927 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8 in Java by Rahul
• 9,680 points
58 views
0 votes
1 answer

__proto__ VS. prototype in JavaScript

To answer your query, the __proto__ is ...READ MORE

answered Feb 22 in Java by Aditya
• 7,660 points
552 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP