February 2018
Intermediate to advanced
298 pages
8h 22m
English
Just like for strings, indexOf exists for arrays as well and as you expect, it'll return the position of the element in the array. Take a look:
const arr = ['apple', 'mango', 'banana'];console.log(arr.indexOf('apple')); // 0console.log(arr.indexOf('mango')); // 1console.log(arr.indexOf('apple') >= 0); // true => apple existsconsole.log(arr.includes('apple')); // true => apple existsconsole.log(arr.indexOf('pineapple') >= 0); // false => pineapple // doesn't existsconsole.log(arr.includes('pineapple')); // false => pineapple doesn't //exists
So what's the difference? There's not really a difference unless we talk about NaN and all that weird stuff. For instance:
const arr = ['Some elements I like', ...
Read now
Unlock full access