1

See the below snippets:

Snippet #1:

let fs = require("fs");
fs.readFile(process.argv[2], "utf8", (error, data) => console.log(arguments));

Snippet #2:

let fs = require("fs");
fs.readFile(process.argv[2], "utf8", (error, data) => console.log(error, data));

Expected log: Values of (error, data), for example like:

null 'console.log("HELLO WORLD");\r\n'


When you try both these snippets, you will find that the Snippet #1 executes and logs some unexpected values for console.log(arguments) but console.log(error, data) logs proper values; values of (error, data).

Why and What was the value that is being logged for Snippet #1?

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
  • Should those snippets be runnable? Coz, `require` will not work – Rajesh Nov 05 '16 at 08:09
  • See also [Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?](http://stackoverflow.com/q/34361379/218196). – Felix Kling Nov 05 '16 at 14:59

2 Answers2

10

No binding of arguments

Arrow functions do not bind an arguments object Thus, arguments is simply a reference to the name in the enclosing scope.

From: MDN - Arrow functions

If you wish to use variadic arguments inside an arrow function, use the rest parameters syntax:

fs.readFile(process.argv[2], "utf8", (...args) => console.log(args));
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97