Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.

Latest commit

 

History

History
27 lines (18 loc) · 885 Bytes

File metadata and controls

27 lines (18 loc) · 885 Bytes

Imagine you have an array of people's names:

var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley"];

You want to check that every student in the array has a name longer than 3 characters. How do you do that for every value in the array?

We can write a function that returns true or false:

function isAboveThreshold(name) {
  return name.length > 3;
}

To check that each name is longer than 3 characters, you'd have to run this function against every name in the array and return false if someone's name is 3 or fewer characters. Thankfully there is an array method that does just this!

.every()

Searches through an array and returns true if every item satisfies the predicate function you provided. Otherwise, it returns false.

var studentNameLength = students.every(isAboveThreshold);

console.log(studentNameLength); // logs true