JavaScript Array map

The JavaScript Array map method applies a callback function to every element of an array.

Syntax [, contextObject])


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

Cross-browser Array map

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

 * Copyright (c) Mozilla Foundation
 * This code is available under the terms of the MIT License
if (! { = function(fun /*, thisp*/) {
        var len = this.length >>> 0;
        if (typeof fun != "function") {
            throw new TypeError();

        var res = new Array(len);
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                res[i] =, this[i], i, this);

        return res;

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