/    Sign up×
Bounties /Pin to ProfileBookmark

How do I check for a specific value inside of an array in JavaScript?

+ 1,000
Copy linkTweet thisAlerts:
Dec 05.2022
to post a answer
Front-endJavaScript

2 Replies

Copy linkTweet thisAlerts:
@lestermDec 06.2022 — To check for a specific value inside of an array in JavaScript, you can use the indexOf() method. This method returns the index of the first occurrence of the value in the array, or -1 if the value is not present in the array.

Here is an example:
let myArray = [1, 2, 3];

if (myArray.indexOf(2) !== -1) {
console.log("2 is in the array");
} else {
console.log("2 is not in the array");
}

In this example, the indexOf() method is used to check if the array contains the value 2. Since 2 is present in the array, the code logs "2 is in the array" to the console.

Alternatively, you can use the includes() method, which returns a boolean value indicating whether the value is present in the array. Here is an example:
let myArray = [1, 2, 3];

if (myArray.includes(2)) {
console.log("2 is in the array");
} else {
console.log("2 is not in the array");
}

In this example, the includes() method is used to check if the array contains the value 2. Since 2 is present in the array, the code logs "2 is in the array" to the console.

Both methods can be used to check for the presence of a specific value in an array.
Copy linkTweet thisAlerts:
@JaySODec 05.2022 — You can use the indexOf JavaScript array method. This method will return the index of the first element in the array that matches the specified value, or -1 if the value is not found in the array.

For example:


const myArray = ['apple', 'banana', 'orange'];

if (myArray.indexOf('banana') !== -1) {
// Value is in the array
} else {
// Value is not in the array
}


In this code, the indexOf method is used to check if the value 'banana' exists in the myArray array. The indexOf method returns the index of the first 'banana' element in the array, which is 1, so the if statement evaluates to true and the code inside the if block is executed.

Alternatively, you can use the includes method of the Array object, which returns a boolean value indicating whether the array includes the specified value. For example:


const myArray = ['apple', 'banana', 'orange'];

if (myArray.includes('banana')) {
// Value is in the array
} else {
// Value is not in the array
}


The includes method is used to check if the value 'banana' exists in the myArray array. The includes method returns true because the 'banana' value exists in the array, so the if statement evaluates to true and the code inside the if block is executed.

The includes method is a simpler and more readable alternative to the indexOf method, but it is not supported in Internet Explorer.
×

Success!

Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.12,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,
)...