Need some help about arrow functions :(

Hey friends when I run console like this " console.log(write(1,2)) => output should be 12. But i couldn’t do it with arrow function. I can do multiplication ,sum etc. but couln’t do that.

Have you looked at the SitePoint article about these? What you’ve posted isnt an arrow function.

1 Like

What do you mean how it isn’t an arrow function?

It isn’t an arrow function because they’re written by assigning an input parameter (or multiple parameters) on the left of the arrow, to a statement or multiple statements on the right of the arrow.

It’s easiest to understand how they’re made by starting from a normal function expression, and notice that the word function is removed when the arrow notation is used.

// function expression
var add = function (a, b) { 
  var result = a + b;
  return result;
};
...
// arrow function
var add = (a, b) => {
    var result = a + b;
    return result;
};

When there’s only one parameter the parenthesis can be removed, and when there’s only one statement in the function the curly braces and the return can be removed too.

// function expression
var double = function (a) { return a * 2; };
...
// arrow function
var double = a => a * 2;
2 Likes

Do you have a problem with arrow functions? Or do you want to know how to concatenate numbers together?

write0 = (a, b) => '' + a + b;
write1 = (a, b) => ''.concat(a, b);
write2 = (...r) => ''.concat(...r);
write3 = (...r) => r.join('');
console.log(write0(1,2,3)); // 12
console.log(write1(1,2,3)); // 12
console.log(write2(1,2,3)); // 123
console.log(write3(1,2,3)); / 123

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.