How to check if element exists in an array with JavaScript

3 Answers

0 votes
let array = [5, 7, 8, 1, 0, 9, 3];

console.log(array.includes(1) ); 
  
    
    
/*
run:
    
true
    
*/

 



answered Jan 25, 2021 by avibootz
0 votes
let array = ["javascript", "php", "ruby", "c++"];

console.log(array.includes("php")); 
  
    
    
/*
run:
    
true
    
*/

 



answered Jan 25, 2021 by avibootz
0 votes
let array = ["javascript", "php", "ruby", "c++"];

if (array.indexOf('php') !== -1) { 
 	console.log("Exist");
}
else {
 	console.log("Not exist");
}
  
    
    
    
/*
run:
    
"Exist"
    
*/

  

 



answered Jan 25, 2021 by avibootz

Related questions

1 answer 136 views
2 answers 238 views
1 answer 142 views
1 answer 131 views
...