I’d like to keep my learning on some JavaScript functions that I learned.
includes()
This method returns true
if an element is included in the array, or it returns false
otherwise. I thought this function is very handy.
const array1 = ['moon', 'sun', 'star'];
console.log(array1.includes('star'));
// Expected output: true
console.log(array1.includes('mars'));
// Expected output: false
- Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
find()
find()
returns the first element in the provided array that matches the given testing function. If no element matches the testing function, undefined
is returned.
const array1 = [{num: 5}, {num: 12}, {num: 8}, {num: 130}, {num: 44}];
const found = array1.find(obj => obj.num > 10);
console.log(found);
// Expected output: Object { num: 12 }
- Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
forEach()
This function performs the provided function to every element of the given array.
const array1 = [1, 2, 3];
array1.forEach(element => console.log(element));
console.log(array1);
// Expected output: "1"
// Expected output: "2"
// Expected output: "3"