0

I'm using regular function give me correct output when I convert it to arrow function give me error /////////////////////////working prefect////////////////////

function sum() {
    total = 0
    for (i in arguments) {
        total += arguments[i]

    }
    console.log(total);
}
sum(20, 80, 90, 70);

output = 260

/////////////not working prefect using arrow function/////////////

var sum = () => {
    total = 0
    for (i in arguments) {
        total += arguments[i]

    }
    console.log(total);
}
sum(20, 80, 90, 70);

Error

0[object Object]function require(path) {
      return mod.require(path);
    }[object Object]c:

1 Answers1

0

Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function. Named and rest parameters are heavily relied upon to capture the arguments passed to arrow functions. In most cases, using rest parameters is a good alternative to using arguments.

var sum = (...arguments) => {
    total = 0;
    for (i in arguments) {
        total += arguments[i]
    }
    console.log(total);
}
sum(20, 80, 90, 70); // returns 260
nurmdrafi
  • 419
  • 4
  • 11