1const functionExpression = function(width, height) {
2 return width * height;
3};
4
5console.log(functionExpression(3, 4));
6// Result = 12
7
8function functionDeclaration(width,height){
9return width * height;
10};
11
12console.log(functionDeclaration(3,4));
13//Result = 12
14
1Example: Function Expression
2alert(foo()); // ERROR! foo wasn't loaded yet
3var foo = function() { return 5; }
4
5Example: Function Declaration
6alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
7function foo() { return 5; }
8
9Function declarations load before any code is executed while Function
10expressionsload only when the interpreter reaches that line of code.
11
12Similar to the var statement, function declarations are hoisted to the
13top of other code. Function expressions aren’t hoisted, which allows
14them to retain a copy of the local variables from the scope where
15they were defined.
1const plantNeedsWater = function(day){
2 if (day === 'Wednesday'){
3 return true;
4 } else{
5 return false;
6 }
7}
8
9console.log(plantNeedsWater('Tuesday'))
1const getRectArea = function(width, height) {
2 return width * height;
3};
4
5console.log(getRectArea(3, 4));
6// expected output: 12
7