Home >Web Front-end >JS Tutorial >js uses for loop to query whether a certain value exists in an array_javascript skills

js uses for loop to query whether a certain value exists in an array_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:39:491730browse

It should be because IE8 does not support indexOf, so write a for loop method to determine whether it exists and return true false

function isCon(arr, val){ 
for(var i=0; i<arr.length; i++){ 
if(arr[i] == val) 
return true; 
} 
return false; 
}

Add this method in js and call the method at the calling location

window.onload = function(){ 
var arr=new Array(1,2,3,2,4,2,5,6,7,2,4,3,18,12,41,87); 
alert(isCon(arr, 8)); 
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn