10

So this code works perfectly

var arr = [1, 2, 3, 4];
arr.forEach(function (el) {
    console.log(el);
})

But if i try to do this:

function printArgsInfo() {
    arguments.forEach(function (el) {
        console.log(el);
    });
}
printArgsInfo(2, 3, 2.5, -110.5564, false);

arguments.forEach is not a function

Even though arguments is an array and if Itry to do this with a for in loop it still works.

2
  • Don't use arguments. It's pretty much obsolete. Commented Jun 8, 2017 at 12:17
  • Replace arguments.forEach... with Array.prototype.slice.call(arguments).forEach... Commented Oct 10, 2017 at 20:09

3 Answers 3

18

arguments is an array-like object, but not an array:

var doThing = function() {
    console.log(arguments.constructor.name)
    console.log([].constructor.name)
}

doThing("someArgument")


Will return Object for arguments and Array for the empty array [].

ES6 and newer

With ES6, you could use rest parameters ..., as torazaburo suggests.

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

function printArgsInfo(...args) {
    args.forEach(el => console.log(el));
}

printArgsInfo(2, 3, 2.5, -110.5564, false);

ES5 and older

For ES5 and older, you could borrow the method from Array#forEach and call it with argument as thisArg.

function printArgsInfo() {
    [].forEach.call(arguments, function (el) {
        console.log(el);
    });
}

printArgsInfo(2, 3, 2.5, -110.5564, false);

Sign up to request clarification or add additional context in comments.

2 Comments

I'm surprised you don't suggest the obvious [...arguments].forEach. Or more preferably, just say function printArgsInfo(...args).
@torazaburo, right, added that as well, thank you for the hint.
7

Per the MDN Docs:

The arguments object is an Array-like object corresponding to the arguments passed to a function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

So, it is not a true array and does not share the Array object's prototype -- which is where the forEach method is defined.

Interestingly, also from the MDN docs:

You can also use the Array.from() method or the spread operator to convert arguments to a real Array

var args = Array.from(arguments);

So, here is a working example with your code:

function printArgsInfo() {
    var args = Array.from(arguments);

    args.forEach(function (el) {
        console.log(el);
    });
}

printArgsInfo(2, 3, 2.5, -110.5564, false);

1 Comment

davidwalsh.name/arguments-array ye, but explains it somewhat beter
4

Even though arguments is an array

It isn't.

function myFunc() {
    console.log(arguments instanceof Array);
}

myFunc(1,2,3);

The Arguments object is an array-like object. It isn't an array.

3 Comments

console.log(typeof arguments); yes low it says its an object and it seems logical not to be albe to foreach it, never tought that it wasn't an array
console.log(typeof []) — arrays are objects (but not all objects are arrays).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.