What is a difference between function declaration and function expression?
A function declaration creates a function in that scope that can be called with a specified name:
function add(x, y) {
return x + y;
}
add(3, 5); // function call
An expression is the function without a name, and it acts as a value in the same way that an object integer or string would, were it can be assigned to a variable:
function (x, y) {
return x + y;
}
Assigning:
const add = function(x, y) {
return x + y;
};
2 Likes
For most of what you are doing they can be considered interchangeable.
Function Declaration
function calcRectArea(width, height) {
return width * height;
}
const getRectArea = function(width, height) {
return width * height;
}
The one of the more practical differences between the two is that the declaration can be hoisted. This means the function can be used before it is formally declared in the code, ie used on a line above the declaration. A function expression cannot be hoisted.
2 Likes