Javascript Function expression

Firstly create a function after that defined into the variable is called function expression.

Syntax:-


var variableName=function(){
}

Note:- You can declare variable through let, var and const according to your requirements.

Example:- Suppose, You create a function and defined into userName variable.


var userName = function() {
  console.log('Hello John');
};

Now, call the userName


userName();
Output:- Hello John

Note:- function expression does not follow Hoisting Rule while Normal function follows the Hoisting Rule.

If you call the function first after that defined the function expression then it will show error.

Example:- Firstly call the userName() function after that defined the userName function expression.


userName();
var userName = function() {
  console.log('Hello John');
};
Output:- Uncaught TypeError: userName is not a function

Normal function follows the Hoisting Rule

Step1:- Firstly call the normal function after that defined userName() function.


userName();
function userName() {
  console.log('Hello John');
};
Output:- Hello John

Step2:- Firstly defined the function after that call the function userName() function.


function userName() {
  console.log('Hello John');
};
userName();
Output:- Hello John