2

What is the difference between these two functions?

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

function square (number) {
  return number * number;
}
Michelle
  • 265
  • 2
  • 14
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions – Yousaf Aug 25 '18 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. – Cheezy Code Aug 25 '18 at 19:17

1 Answers1

2

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.

Paul
  • 35,689
  • 11
  • 93
  • 122