JavaScript Array some

The JavaScript Array some method checks whether some elements of an array pass the test implemented by the callback function.

Syntax

array.some(callback [, contextObject])

Parameters

callback
The callback function to test each element
contextObject
Object to be used as a context for the callback function

Cross-browser Array some

For browsers that don't support the Array some method, here's an alternative implementation:

/**
 * Copyright (c) Mozilla Foundation http://www.mozilla.org/
 * This code is available under the terms of the MIT License
 */
if (!Array.prototype.some) {
    Array.prototype.some = function(fun /*, thisp*/) {
        var i = 0, len = this.length >>> 0;
        if (typeof fun != "function") {
            throw new TypeError();
        }

        var thisp = arguments[1];
        for (; i < len; i++) {
            if (i in this && fun.call(thisp, this[i], i, this)) {
                return true;
            }
        }

        return false;
    };
}

See also

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript