Arrays in JavaScript — Part 2

📄 Table of Contents

  • some() in Array

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ ✦ ✦ ✦ ▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Array find() Method

var ages = [3, 10, 18, 20];

function checkAdult(age) {
return age >= 18;
}

function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAdult);
}

The find() method executes the function once for each element present in the array.

Note: find() does not change the original array.

Array some() Method

  • it returns a boolean value. If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)
  • Otherwise it returns false

Note: some() does not change the original array.

Array.fill()

Array.from()

Array.find()

Array.findIndex()

Array.of()

When to use forEach?

When to use map?

When to use filter?

When to use find?

When to use reduce?

Quirks and Criticisms

forEach returns undefined.

Example:

const arr = [1,2,3];const transformedArr = arr.map(function(){}).filter(function(){});

A word about speed.

In reality, you shouldn’t be using .forEach() unless other methods such as .map() can’t do the trick. .map() is actually slightly faster than .forEach().

Admittedly, .forEach() and .map() are still slower than a vanilla for loop. But judging a method solely based on execution speed is tunnel-visioned. This argument completely ignores readability and scope.

The key takeaway here is don’t use for loops because you think they are faster, use them when you know you need to.

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store