JavaScript functions for array

created:

updated:

tags: javascript

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

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 }

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"

References