Function Return Statements 2

Video

JavaScript Notes

JavaScript
    // What do functions really return? And why?
    const log = console.log;
    
    let app = null;
    
    app = (function() {
      //this is an IIFE - Immediately Invoked Function Expression
      //it runs and then it's return value is passed to the variable app
      return {
        buybeer: function(money) {
          if (money >= 3) {
            log("your change is", money - 3, "and here is your beer");
            return true;
          }
          // else {
          //   return false;
          // }
        }
      };
    })();

    app.buybeer(4); // this returns 1
    
    let hadEnough = app.buybeer(2);
    if (hadEnough) {
      log("They had enough money");
    } else {
      log("Not enough money");
    }
    // log(app);