📜  JavaScript 中的 every() 和 some() 方法有什么区别?

📅  最后修改于: 2022-05-13 01:56:22.229000             🧑  作者: Mango

JavaScript 中的 every() 和 some() 方法有什么区别?

JavaScript 中的Array.every()方法用于检查数组的所有元素是否满足给定条件。

JavaScript 中的Array.some()方法用于检查数组的至少一个元素是否满足给定条件。唯一的区别是,如果任何谓词为真,则some()方法将返回真,而如果所有谓词为真,则every()方法将返回真。

示例 1:此示例实现了 some() 方法。

javascript


javascript


输出:

true

示例 2:此示例实现了 every() 方法。

javascript


输出:

false

让我们看看表格形式的差异-:

 Array.every()Array.some()
1.The Array.every() method is used to check whether all the elements of the array satisfy the given condition or not.The Array.some() method is used to check whether at least one of the elements of the array satisfies the given condition or not.
2.The  some() method will return true if any predicate is trueThe every() method will return true if all predicates is true
3.This method executes a function for each array element.This method does not execute the function for empty array elements.
4.This method does not execute the function for empty elements.This method does not change the original array.
5.This method does not change the original arrayIts return value is of Boolean type
6.

Its Syntax is -: 

array.every(function(value, index, array), thisValue)

Its syntax is -:

array.some(function(value, index, array), this)

7.

Its supported browsers are -:

Chrome, Internet Explorer 9 – 11, Firefox, Safari, Microsoft Edge, Opera

Its supported browsers are -:

Chrome, Internet Explorer, Firefox, Safari, Microsoft Edge, Opera