-1

Is there any difference in these two?

EDIT (added return):

const square = function (a) {
result = a ** 2
return result}
function square(a) { a ** 2
result = a ** 2
return result}

It looks different and in the first example I have an anonymous function on the right side.

But after they are implemented I can both call them with e.g. console.log(square(5)), right?

What's the difference?

  • You can _call_ them, sure, but neither _returns_ anything. I'd suggest reading e.g. https://stackoverflow.com/q/34361379/3001761 (and [ask]). – jonrsharpe May 05 '22 at 22:25
  • In your example there's an essential difference: with `const squre = function(a) { return a**2 }` you cannot accidentally or intentionally overwrite `square`, which is something you cannot achieve using just a named function declaration. On the other hand, using a function declaration allows calling the function before declaring it. – connexo May 05 '22 at 22:43

0 Answers0