How to check whether an array includes a certain value in JavaScript

4 Answers

0 votes
var arr = [1, 2, 3, 4, 5];
 
if (arr.includes(2))
    document.write("yes");
else
    document.write("no");
 
/*
run:  
 
yes 
 
*/

 



answered May 19, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
if (arr.includes(7))
    document.write("yes");
else
    document.write("no");
 
/*
run:  
 
no  
 
*/

 



answered May 20, 2016 by avibootz
0 votes
// var boolean = array.includes(searchElement[, fromIndex])

var arr = [1, 2, 3, 4, 5];
 
if (arr.includes(4, 3))
    document.write("yes");
else
    document.write("no");
 
/*
run:  
 
yes   
 
*/

 



answered May 20, 2016 by avibootz
0 votes
// var boolean = array.includes(searchElement[, fromIndex])

var arr = [1, 2, 3, NaN, 5];
 
if (arr.includes(NaN))
    document.write("yes");
else
    document.write("no");
 
/*
run:  
 
yes   
 
*/

 



answered May 20, 2016 by avibootz
...