An Interest In:
Web News this Week
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
- March 13, 2024
July 9, 2021 09:26 pm GMT
Original Link: https://dev.to/rthefounding/using-the-every-method-in-an-array-1j64
Using the every Method in an array
The
every
method works with arrays to check if every element passes a particular test. It returns a Boolean valuetrue
if all values meet the criteria, false if not.Example, the following code would check if every element in arr is positive.
function check(arr) {return arr.every(function(num) { return num > 0;})}console.log(check([1, 2, 3, -4, 5]));
check([1, 2, 3, -4, 5])
should returnfalse
Using the some Method
- The
some
method works with arrays to check if any element passes a particular test. It returns a Boolean valuetrue
if any of the values meet the criteria, false if not. - Ex:
function check(arr) {return arr.some(function(num) { return num > 0;})}console.log(check([1, 2, 3, -4, 5]));// would return true
Original Link: https://dev.to/rthefounding/using-the-every-method-in-an-array-1j64
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To