Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Created April 20, 2016 14:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markhowellsmead/7fa5ad4a7d2eb6245cae13942da78b6a to your computer and use it in GitHub Desktop.
Save markhowellsmead/7fa5ad4a7d2eb6245cae13942da78b6a to your computer and use it in GitHub Desktop.
Polyfill JavaScript Array.prototype.find for older browsers (e.g. IE 10, IE 11)
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
@HFTSxRiDeR
Copy link

HFTSxRiDeR commented Oct 5, 2018

Why do you use list.length >>> 0 and return undefined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment