ibvasi
11/26/2016 - 12:25 AM

Passing functions to variables

Passing functions to variables

/* Passing functions to variables * /

/* Function definitions */
function greet() {
    alert("Hi"); // alerts Hi
}
var greetOne = greet;
greetOne();

function add(a, b) {
    return a + b;
}
var c = add(1, 2);
console.log(c); // 3

var c = add;
console.log(c(1, 2)); // 3

/* Function expressions */
var f = function add(d, e) {
    return d + e;
}
console.log(f(1, 2)); // 3

var Message = function Message() {
    alert("Hi I worked");
}
Message(); // alerts Hi I worked

var MessageOne = function MessageOne() {
    alert("Hi, I worked too");
}
var greeting = MessageOne;
greeting(); // alerts Hi, I worked too