The Variable Declaration - const

Video

JavaScript Notes

JavaScript
    // Why Should I Use const?
    
    const log = console.log;
    
    const obj = {
      a: 123
    };
    obj.b = "hello";    //can still add new properties
    obj.a = 423;        //can still change properties
    delete obj.a;       //can still delete properties
    
    const f = function() {};
    
    // using const ensures these cannot later be overwritten
    // you could also use property descriptors instead
    const mediaTypes = { AUDIO: 0, VIDEO: 1, PNG: 2, JPEG: 3, WEBP: 4 };
    const errorTypes = { NOTPAID: 0, NOTAVAILABLE: 1, PAINTBALL: 2 };
    
    let myObj = {
      doSomething: function() {
        //do something
        //but an error happens
        let num = Math.floor(Math.random() * 3);
        switch (num) {
          case 0:
            log("Error Code ", errorTypes.NOTPAID);
            break;
          case 1:
            log("Error Code ", errorTypes.NOTAVAILABLE);
            break;
          case 2:
            log("Error Code ", errorTypes.PAINTBALL);
            break;
        }
      },
      saveMedia: function(type, data) {
        switch (type) {
          case 0:
            log("saving an audio file");
            break;
          case 1:
            log("saving a video file");
            break;
          case 2:
          case 3:
          case 4:
            log("saving an image");
            break;
        }
      }
    };
    
    //myObj.doSomething();
    myObj.saveMedia(0, "audio.mp3");
    myObj.saveMedia(mediaTypes.AUDIO, "audio.mp3");
    myObj.saveMedia(mediaTypes.JPEG, "img.jpg");