What is the difference between these two functions?
const square = (number) => {
return number * number;
};
function square (number) {
return number * number;
}
What is the difference between these two functions?
const square = (number) => {
return number * number;
};
function square (number) {
return number * number;
}
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.