-2

What is the difference between these functions:

callback(x: string[], y: string[]){
  console.log(x.concat(y).join(', '));
}

and

(c,d) => console.log(c.concat(d).join(', '))

?

Marek
  • 27
  • 5

2 Answers2

1

From theoretical view first is a standard function, the second is so called arrow function.

From the Documentation, arrow functions differs from standard functions in this criterias

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

  • Arrow functions does not have their own this, they get it from the upper scope, where are defined.

  • They don't have arguments object inside the body, which has every
    standard function and via that you can access passed in arguments.

  • You can create an object via arrow function - see point 1.

From Typescript view, same as above and also that you haven't provide argument types for the arrow function. Also be aware that you mistyped argument name in the arrow function.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

The first is TypeScript and performs type checking on the inputs.

The second is JavaScript and throws an error because the variable names in the arguments don't match the variable names used in the function. (That error aside, it does the same thing, just without the type checking).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335