(If you know C# LINQ , it's like Any vs All)

  • some will return true if any predicate is true

  • every will return true if all predicate is true

Where predicate means function that returns bool ( true/false) for each element

every returns on first false.
some returns on first true

Answer from Royi Namir on Stack Overflow
๐ŸŒ
Go Make Things
gomakethings.com โ€บ why-would-you-use-array.some-or-array.every-over-array.filter
Why would you use Array.some() or Array.every() over Array.filter() | Go Make Things
Array.some() returns true if at least one item in your array matches some criteria you specify as part of a callback function. Array.every() returns true if all items in your array matches some criteria you specify as part of a callback function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ what-is-the-difference-between-every-and-some-methods-in-javascript
What is the difference between every() and some() methods in JavaScript ? - GeeksforGeeks
July 12, 2025 - In this article, we will see the difference between every() and some() methods in JavaScript. The Array.every() method in JavaScript is used to check whether all the elements of the array satisfy the given condition or not.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ some
Array.prototype.some() - JavaScript | MDN
false unless callbackFn returns a truthy value for an array element, in which case true is immediately returned. The some() method is an iterative method. It calls a provided callbackFn function once for each element in an array, until the callbackFn returns a truthy value.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-are-the-every-and-some-methods-in-javascript
What are the every() and some() methods in JavaScript?
The every() array method in JavaScript is used to check if all the elements of the array satisfy the callback function condition or not. The some() array method in JavaScript is used to check if at least one of the elements passes the callback ...
๐ŸŒ
DEV Community
dev.to โ€บ amirfakour โ€บ understanding-some-and-every-array-methods-in-javascript-59fh
Understanding "some" and "every" Array Methods in Javascript - DEV Community
June 12, 2023 - In this example, the some method returns true because the number 12 is greater than 10. The Array.every() method tests if all elements in an array pass the provided test function.
๐ŸŒ
YouTube
youtube.com โ€บ watch
#52 Array.some() & Array.every() in JavaScript - JavaScript Array Methods - YouTube
In this video, we are going to talk about the some and the every methods of arrays in JavaScript. We can use the some and every methods to see if a certain c...
Published ย  March 19, 2024
๐ŸŒ
Linux Hint
linuxhint.com โ€บ difference-between-every-and-some-methods-javascript
Difference between every() and some() methods in ...
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ every
Array.prototype.every() - JavaScript | MDN
true unless callbackFn returns a falsy value for an array element, in which case false is immediately returned. The every() method is an iterative method. It calls a provided callbackFn function once for each element in an array, until the callbackFn returns a falsy value.
Find elsewhere
๐ŸŒ
Marius Schulz
mariusschulz.com โ€บ blog โ€บ the-some-and-every-array-methods-in-javascript
The some() and every() Array Methods in JavaScript โ€” Marius Schulz
November 22, 2020 - Similar to some(), the execution of every() is short-circuited. As soon as every() finds an array element that doesn't match the predicate, it immediately returns false and doesn't iterate over the remaining elements.
๐ŸŒ
Medium
medium.com โ€บ coding-in-depth โ€บ when-to-use-every-some-any-foreach-and-map-in-javascript-9ed598010946
When to use every(), some(), any(), forEach() and map() in JavaScript | by Coding In depth | Coding In Depth | Medium
August 8, 2020 - Is Array.any() exist in the JavaScript? The answer is no. However, Array.some() do the purpose of getting any elements. The method Some will return true when the first matching condition will be met.
๐ŸŒ
DEV Community
dev.to โ€บ nas5w โ€บ learn-the-javascript-array-every-and-array-some-methods-356c
Learn the JavaScript Array.every() and Array.some() Methods - DEV Community
June 9, 2020 - Since none of the elements are greater than 10, Array.some returns false after testing each element. ... Not only does it return true, it returns true as soon as the first element passes the test. In this cases, since the number 4 passes the test, 5 and 6 aren't even tested. Now that we generally know how the every and some methods work, here are some additional functionality you can get out of them:
๐ŸŒ
YouTube
youtube.com โ€บ watch
The JavaScript Array.some and Array.every Methods - YouTube
Please subscribe: https://www.youtube.com/channel/UC-3WU7dH0dvZ5BkSSI7zK_w?sub_confirmation=1JS + TS mailing list: https://buttondown.email/devtutsWebsite: h...
Published ย  June 9, 2020
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ javascript-array-some-and-every-2975e0cc12f0
JavaScript Array.some() and every() | by Sateesh Gandipadala | JavaScript in Plain English
December 27, 2019 - The some() method stops executing ... This is exactly the opposite of some method. Every() method tests if every element in the array are the same kind we are testing for....
๐ŸŒ
Refine
refine.dev โ€บ home โ€บ blog โ€บ tutorials โ€บ a definitive guide on javascript every method
A Definitive guide on JavaScript every Method | Refine
January 17, 2025 - No, the method itself doesn't change the array. However, the array can change inside the callback function. every checks if all elements meet the condition, while some checks if at least one element meets the condition.
Top answer
1 of 3
4

This is essentially a current limitation in TypeScript; see microsoft/TypeScript#44373.


TypeScript has long had a problem with calling methods on a union of array types. Originally you couldn't call unions-of-functions at all, because the compiler didn't know how to merge multiple signatures into a single usable one. See microsoft/TypeScript#7294:

// Before TS3.3:
declare const arr: string[] | number[];
arr.some(() => true); // error
arr.map(() => 1); // error
arr.every(() => true); // error

TypeScript 3.3 introduced some support for doing this with microsoft/TypeScript#29011, by accepting an intersection of the parameters from the union members.

But this support was only added for relatively simple cases where at most one member of the union was a generic function and at most one member of the union was an overloaded function. So if the array methods you were merging were generic or overloaded, you still couldn't call them. So there was an improvement, but some methods like map() still weren't callable. See microsoft/TypeScript#36390:

// TS3.3
declare const arr: string[] | number[];
arr.some(() => true); // okay
arr.map(() => 1); // error
arr.every(() => true); // okay

At some point it was decided to add a generic overload to every() so that it could act as a type guard function; see microsoft/TypeScript#38200. This is useful, but unfortunately it means that every() also stopped working on unions of array types. This was released with TypeScript 4.0:

// TS 4.0
declare const arr: string[] | number[];
arr.some(() => true); // okay
arr.map(() => 1); // error
arr.every(() => true); // error

For TypeScript 4.2 microsoft/TypeScript#31023 was merged, which added some support for calling unions of generic call signatures, as long as the generic type parameters were identical. But for overloaded call signatures, the problem persists:

// TS 4.2+
declare const arr: string[] | number[];
arr.some(() => true); // okay
arr.map(() => 1); // okay
arr.every(() => true); // error 

And that's where we are currently.


Maybe at some point, microsoft/TypeScript#44373 will be addressed. It's not obvious what the right thing to do here is for unions of overloaded methods in general. I think it's possible someone might want to target just the unions-of-array case by automatically widening to a readonly-array-of-unions, since doing that manually is the workaround I usually suggest, and it's fairly safe:

const arr2: readonly (string | number)[] = arr; // okay
arr2.every(() => true); // okay

But for now, this doesn't happen.

Playgorund link to code

2 of 3
2

As we already discussed in the comments, every has an overload containing a generic function with a type predicate while some has no such things.

Why are the function signatures "incompatible" now?

  • Its definitely related to the fact that they are generic. I have yet to find a definitive answer. Maybe @jcalz knows a satisfying explanation :)

Why is every generic and has a type predicate?

  • Contrary to some, calling every can have an effect on the type of the array. If you have an array like

    const arr: (string | number)[] = []
    

    Calling every on it and checking if every element is a number should also change the type of the resulting array.

    const arr2 = arr.every((e): e is number => typeof e === "number")
    // arr2 should be number[]
    

    This is only possible to achieve with type predicates. And to allow type predicates to have an effect here, every must be generic. (Otherwise the type predicate would be ignored by every)


So what can you do about it?

I would suggest changing the type of input to an intersection of (number[] | string[]) and (number | string)[]. This may look weird but it does fix the error here.

function doEverythingWithArray(
  input: (number[] | string[]) & (number | string)[]
): boolean {
  return input.every(i => doSomething(i));
}

The function also remains callable as before.

// works as before
doEverythingWithArray(["a", "b", "c"])
doEverythingWithArray(["a", "b", "c"] as string[])
doEverythingWithArray([0, 1, 2])
doEverythingWithArray([0, 1, 2] as number[])

// fails
doEverythingWithArray([0, 1, "a"]) 

Playground

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ learn-the-every-and-some-array-methods-in-javascript
JavaScript Array Methods โ€“ How to Use every() and some() in JS
August 10, 2022 - And in that case every returns false โ€“ otherwise it returns true. ... For each iteration, it calls the given function with the current array element as its 1st argument. The loop continues until the function returns a truthy value. And in that case some returns true โ€“ otherwise it returns false.
Top answer
1 of 2
10

If I understand your point correctly, you seem to be mis-using or abusing every and some but it's a little unavoidable if you want to change the elements of your arrays directly. Correct me if I'm wrong, but what you're trying to do is find out if some or every element in your sequence exhibits a certain condition then modify those elements. Also, your code seems to be applying something to all items until you find one that doesn't pass the predicate and I don't think that's what you mean to be doing. Anyways.

Let's take your first example (slightly modified)

if (input.every(function (that) {
    return typeof that === "number";
})) {
    input.every(function (that) {
        that.foo();
    }
} else {
    return;
}

What you're doing here actually goes a little against the spirit of the some/every/map/reduce/filter/etc concepts. Every isn't meant to be used to affect every item that conforms to something, rather it should only be used to tell you if every item in a collection does. If you want to apply a function to all items for which a predicate evaluates to true, the "good" way to do it is

var filtered = array.filter(function(item) {
    return typeof item === "number";
});

var mapped = filtered.map(function(item) {
    return item.foo(); //provided foo() has no side effects and returns a new object of item's type instead.  See note about foreach below.
});

Alternatively, you could use foreach instead of map to modify the items in-place.

The same logic applies to some, basically:

  • You use every to test if all elements in an array pass some test.
  • You use some to test if at least one element in an array passes some test.
  • You use map to return a new array containing 1 element (which is the result of a function of your choice) for every element in an input array.
  • You use filter to return an array of length 0 < length < initial array length elements, all contained in the original array and all passing the supplied predicate test.
  • You use foreach if you want map but in-place
  • You use reduce if you want to combine the results of an array in a single object result (which could be an array but doesn't have to).

The more you use them (and the more you write LISP code), the more you realize how they are related and how it's even possible to emulate/implement one with the others. What's powerful with these queries and what's really interesting is their semantics, and how they really push you towards eliminating harmful side-effects in your code.

EDIT (in light of comments): So let's say you want to validate that every element is an object and convert them to an Application Model if they're all valid. One way to do this in a single pass would be:

var dirty = false;
var app_domain_objects = input.map(function(item) {
    if(validate(item)) {
        return new Model(item);
    } else {
        dirty = true; //dirty is captured by the function passed to map, but you know that :)
    }
});
if(dirty) {
    //your validation test failed, do w/e you need to
} else {
    //You can use app_domain_objects
}

This way, when an object doesn't pass validation, you still keep iterating through the entire array, which would be slower than just validating with every. However, most of the time your array will be valid (or I should hope so), so in most cases you'll perform a single pass over your array and end up with a usable array of Application Model objects. Semantics will be respected, side-effects avoided and everyone will be happy!

Note that you could also write your own query, similar to foreach, which would apply a function to all members of an array and returns true/false if they all pass a predicate test. Something like:

function apply_to_every(arr, predicate, func) {
    var passed = true;
    for(var i = 0; i < array.length; ++i) {
        if(predicate(arr[i])) {
            func(arr[i]);
        } else {
            passed = false;
            break;
        }
    }
    return passed;
}

Although that would modify the array in place.

I hope this helps, it was very fun to write. Cheers!

2 of 2
-1

The side effects are not in the if condition, they are in the if's body. You have only determined whether or not to execute that body in the actual condition. There is nothing wrong with your approach here.

๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_some.asp
JavaScript Array some() Method
The some() method executes the callback function once for each array element.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ java-script-bits-array-methods-some-vs-every-
Java Script Bits - Array Methods - some vs every vs find vs filter
August 12, 2022 - Above is what Array.every does. It iterates through the array to find apple, and as soon as it finds one, it returns the apple.