Function Arguments object in Node.js is different to Chrome’s JavaScript

Function Arguments object in Node.js is different to Chrome’s JavaScript

Problem

I’m confused with Node.js function arguments object.

Suppose i have following code:

function x() {
  return arguments;
}

console.log(x(1, 2, 3));

In chrome developer tools it returns as an Array:

[1, 2, 3]

But i got different result in node.js:

{ '0': 1, '1': 2, '2': 3 }

How come?

Problem courtesy of: user1725316

Solution

arguments is a magic variable that isn’t actually an Array. It behaves like an Array, but it doesn’t have all the functions that an Array has.

Other objects like this are NodeList for example.

Solution courtesy of: Halcyon

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *