kaniosrn-j
11/4/2017 - 12:14 PM

Callbacks

Simple callbacks example

// Example 1
var ifElse = function( condition, isTrue, isFalse) {
  if(condition){
    isTrue();
  }else {
    isFalse();
  }
  return;
}

isTrue = function(){ console.log(true); };
isFalse = function(){ console.log(false);};

ifElse( true, isTrue, isFalse);


// Example 2
var increment = function (n) { return n + 1; }
var square = function (n) { return n * n ;}

var doMathSoIDontHaveTo = function(n,cb){
  return cb(n);
}

var incrementResult = doMathSoIDontHaveTo(4,increment); // 5
var squareResult = doMathSoIDontHaveTo(5, square); // 25

console.log( incrementResult, squareResult);