Answers for "function expression vs function declaration"

8

function expression vs function declaration

const functionExpression = function(width, height) {
  return width * height;
};

console.log(functionExpression(3, 4));
// Result = 12

function functionDeclaration(width,height){
return width * height;
};

console.log(functionDeclaration(3,4));
//Result = 12
Posted by: Guest on September-11-2020
1

function expression vs function declaration

Example: Function Expression
alert(foo()); // ERROR! foo wasn't loaded yet
var foo = function() { return 5; }

Example: Function Declaration
alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
function foo() { return 5; }

Function declarations load before any code is executed while Function
expressionsload only when the interpreter reaches that line of code.

Similar to the var statement, function declarations are hoisted to the
top of other code. Function expressions aren’t hoisted, which allows 
them to retain a copy of the local variables from the scope where
they were defined.
Posted by: Guest on September-13-2021
3

js function expression

const plantNeedsWater = function(day){
  if (day === 'Wednesday'){
    return true;
  } else{
    return false;
  }
}

console.log(plantNeedsWater('Tuesday'))
Posted by: Guest on August-07-2020
2

function expression

const getRectArea = function(width, height) {
  return width * height;
};

console.log(getRectArea(3, 4));
// expected output: 12
Posted by: Guest on July-05-2020

Code answers related to "function expression vs function declaration"

Code answers related to "Javascript"

Browse Popular Code Answers by Language