Function Expression vs Function Declaration in JavaScript?


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;
}

Rishi Raj
Rishi Raj

I am a coder

Updated on: 12-Jun-2020

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements