Javascript array some() function tests whether some item in the array passes the test implemented by the provided function. The some() method is the JavaScript extension to the ECMA-262 standard; as such, it may not be present in other implementations of the standard.

JavaScript Array Some Example

JavaScript array some() is the inbuilt method that tests whether at least one item in the array passes the test implemented by a provided function. It returns the Boolean value.

The some() method in JavaScript executes the function once for each element present in the array:

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

Syntax

array.some(function(currentValue, index, arr), thisValue)

Parameters

JavaScript some() function takes the first parameter as a function that takes the following parameters.

  1. currentValue: It is Required. The value of the current element.
  2. index: It is Optional. The array index of the current element.
  3. arr: It is Optional. The array object the current element belongs to.

The function is the required parameter.

It takes thisValue as a second parameter, which is optional. It is the value to be passed to the function to be used as its “this” value. If the thisValue parameter is empty, the value “undefined” will be given as its “this” value.

Return Value

Javascript some() method returns true if the callback function returns a truthy value for at least one element in the array. Otherwise, it returns false.

Example

Write the following code inside the new file. Let’s call it the app.js file.

// app.js

let dark = [
  100,
  90,
  80,
];

console.log(dark.some(x => x > 90));

Output

true

In this example, we defined an array and then check if the array contains a single item that satisfies the condition, and if it does, then returns true otherwise, it returns false.

#javascript #array #programming

JavaScript Array Some Example | Array.prototype.some()
16.70 GEEK