JS Array Some & Every

Array some()

The Javascript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method. 

Syntax

arr.some(callback(element,index,array),thisArg)

Parameters: This method accepts five parameters as mentioned above and described below:

Return value: This method returns true even if one of the elements of the array satisfies the condition(and does not check the remaining values) implemented by the argument method. If no element of the array satisfies the condition then it returns false.
 

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var ages = [10, 13, 18, 2];
    document.write(ages + "<br>");

    var b = ages.some(checkAdult);
    document.write(b + "<br>");
  </script>
</head>
<body>

</body>
</html>

Array every()

The Javascript Array.every() method considers all the elements of an array and then further checks whether all the elements of the array satisfy the given condition (passed by in user) or not that is provided by a method passed to it as the argument.

Syntax:

// Arrow function
every((element) => { /* … */ })
every((element, index) => { /* … */ })
every((element, index, array) => { /* … */ })

// Callback function
every(callbackFn)
every(callbackFn, thisArg)

// Inline callback function
every(function (element) { /* … */ })
every(function (element, index) { /* … */ })
every(function (element, index, array) { /* … */ })
every(function (element, index, array) { /* … */ }, thisArg)

Parameters: This method accepts five parameters as mentioned above and described below:

Return value: This method returns a Boolean value true if all the elements of the array follow the condition implemented by the argument method. If any one of the elements of the array does not satisfy the argument method, then this method returns false.
 

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var ages = [10, 19, 18, 20];
    document.write(ages + "<br>");

    var b = ages.every(checkAdult);
    document.write(b + "<br>");
  </script>
</head>
<body>

</body>
</html>