Function Expression vs Function Declaration in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Function Declaration

The “function” keyword declares a function in JavaScript. To define a function in JavaScript use the “function” keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.

Here’s an example −

function sayHello(name, age) {
   document.write (name + " is " + age + " years old.");
}

Function Expression

Function Expression should not start with the keyword “function”. Functions defined can be named or anonymous.

Here are the examples −

//anonymous function expression
var a = function() {
   return 5;
}

Or

//named function expression
var a = function bar() {
   return 5;
}
raja
Published on 29-Jan-2018 10:07:43
Advertisements