For example i have an array with 100 values, let it be a[1,2,3...100]
Can I create a if check that will be suitable for any value from the array?
like:
if(value === a[any index]){
//do smth
}
almost sure that it can be done but don't know how, can you help me with that?
// for of
for (const item of a) {
if (value === item) {
}
}
// traditional for loop
for (let i = 0; i < a.length; i++) {
if (value === array[i]) {
}
}
// forEach
a.forEach(item => {
if (value === array[i]) {
}
});
You should use the ES7 array.includes(). If it finds a given value in your array it will return a True boolean, else it will return False.
Example:
console.log([1,2,3].includes(2)) //True
console.log([1,2,3,4,5].includes(6)) //False