-2

i have to turn an arrow function to a regular one. The problem is that the arrow function is pretty different from those i'm used to work with hence harder to transform. I need your help please. Here is the arrow function followed by the regular function i tried to code. The functions :

(name) => {
    `Bonjour, ${name} ! Comment vas-tu ?`;
}
taille = () => 
{

function(name)
{
return `Bonjour, ${name} ! Comment vas-tu?`;
}
  • 1
    Whats the `taille = () => {` part? – 0stone0 Apr 29 '22 at 15:30
  • why you want to do that ? an arrow function is fine it's also a function, and also you have a return in one and not the other – Lk77 Apr 29 '22 at 15:31
  • It looks like your arrow function does absolutely nothing. – nubinub Apr 29 '22 at 15:31
  • @Lk77 It's presumably an exercise to understand the different syntaxes. – Barmar Apr 29 '22 at 15:33
  • i think you are confused by the {} part, you need a return if you use {}, you can omit the return when doing `(e) => e.length` for example but not with {} – Lk77 Apr 29 '22 at 15:34
  • Does this answer your question? [Converting ECMAScript 6's arrow function to a regular function](https://stackoverflow.com/questions/37668185/converting-ecmascript-6s-arrow-function-to-a-regular-function) – 0stone0 Apr 29 '22 at 15:40
  • Does this answer your question? [Es6 Arrow function to normal js](https://stackoverflow.com/questions/40341121/es6-arrow-function-to-normal-js) – 0stone0 Apr 29 '22 at 15:42

1 Answers1

1

The equivalent regular function is almost identical to the arrow function.

function(name) {
  `Bonjour, ${name} ! Comment vas-tu?`;
}

When an arrow function's body is surrounded by {}, it's the same as the body of a regular function. The differences between these types of arrow and regular functions are related to the scope of this and the availability of the special variable arguments, but these are not relevant in your example.

Note that this function is not very useful, since the string is never returned.

An arrow function only has an implicit return if the body is not surrounded by {}. Your regular function is equivalent to this:

(name) => `Bonjour, ${name} ! Comment vas-tu ?`
Barmar
  • 741,623
  • 53
  • 500
  • 612