2

What is the difference between these two functions?

const square = (number) => {
  return number * number;
};

function square (number) {
  return number * number;
}
2
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions Commented Aug 25, 2018 at 19:10
  • Your function will never get overridden by some other js file having same function name in case you declare it as const. The definition will remain exactly the same, whatever be the case. Commented Aug 25, 2018 at 19:17

1 Answer 1

3

There are several.

First, const prevents reassignment of the name square while function does not. Second, using an arrow function doesn't have it's own lexical context, so it won't have a scoped this and can't be used as a constructor. For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Note you can also do:

  const square = function(num) { return num * num }

Which both prevents reassignment and creates a lexical context.

Sign up to request clarification or add additional context in comments.

4 Comments

Arrow functions create a new scope, otherwise the var in () => {var a =8} would be accessible outside of the arrow function.
Sorry, meant lexical context, not scope. Fixed @t.niese
What do you mean by lexical context?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.