How to check if an array contains a specific value in JavaScript

5 Answers

0 votes
const arr = [1, 3, 8, 9, 17, 18];

console.log(arr.includes(8)); 





/*
run:

true

*/

 



answered Feb 14, 2021 by avibootz
0 votes
const arr = [1, 3, 8, 9, 17, 18];

console.log(!!arr.find((n) => n === 8)); 





/*
run:

true

*/

 



answered Feb 14, 2021 by avibootz
0 votes
const arr = [1, 3, 8, 9, 17, 18];

console.log(arr.indexOf(8) !== -1); 





/*
run:

true

*/

 



answered Feb 14, 2021 by avibootz
0 votes
const arr = [1, 3, 8, 9, 17, 18];

console.log(arr.findIndex((element) => element === 8) !== -1); 





/*
run:

true

*/

 



answered Feb 14, 2021 by avibootz
0 votes
const arr = [1, 3, 8, 9, 17, 18];

console.log(arr.some((element) => element === 8)); 





/*
run:

true

*/

 



answered Feb 14, 2021 by avibootz
...