ECMAScript question detail
Array find methods
ES6 introduced few array methods and two of them are Array.find() and Array.findIndex().
Array.find()
This method returns the value of the first element in an array that satisfies the given test. Let's take an example of array with all even elements except one element and use find method to find the odd element.
let arr = [2, 4, 5, 6, 8, 10];
function isOdd(i) {
return i % 2 !== 0;
}
console.log(arr.find(isOdd)); // 5
Array.findIndex()
This method returns the index of the first element in the array that satisfies the given test. Let's take an example of array with all even elements except one element and use findIndex method to find the index of odd element.
let arr = [2, 4, 5, 6, 8, 10];
function isOdd(i) {
return i % 2 !== 0;
}
console.log(arr.findIndex(isOdd)); //2
ES2016 Or ES7
ES2015/ES6 introduced a huge set of new features. But ECMAScript 2016 Or ES7 introduced only two new features:
- Array.prototype.includes()
- Exponentiation operator