Search This Blog

Monday, 6 March 2017

Function Expression and Anonymous Function in javascript

JavaScript allows us to assign a function to a variable and then use that variable as a function. It is called function expression.
Example: Function expression

var Subtraction = function sub(val1, val2) {
    return val1 - val2;

var result1 = Subtraction (10,20);
var result2 = sub(10,20); // not valid

JavaScript allows us to define a function without any name. This unnamed function is called anonymous function. Anonymous function must be assigned to a variable.
Example: Anonymous Function

var showMessageSample = function (){
    alert("Welcome to javascript function!");


var sayHi = function (firstName) {
    alert("Hello " + firstName);

showMessageSample ();


No comments:

Post a Comment