Skip to content

Instantly share code, notes, and snippets.

@abhisekp
Last active May 10, 2018 04:30
Show Gist options
  • Save abhisekp/80ef2b34ca0008778c303b25a6fe788d to your computer and use it in GitHub Desktop.
Save abhisekp/80ef2b34ca0008778c303b25a6fe788d to your computer and use it in GitHub Desktop.
Variable Declarations in JavaScript — http://bit.ly/js-vars

Variable Declarations

  1. function param
    implicit local variable declaration

    function sum(num) {
      // `num` is a local variable
    }
  2. using var keyword
    explicit local variable declaration inside a function

    function getMe() {
     var answer = 'no';
    }

    or implicit global variable declaration when declared out of any function context

    var age = 150; // global
  3. using const keyword (similar to let)
    implicit global variable declaration when declared out of any function context

    const name = 'Abhisek';
  4. without using var, const or let keywords (JS being overfriendly)
    implicitly creates a global variable (bad!!!) ⚠️ (doesn't work in strict mode) 👿

    function me() {
     answer = 'Abhisek';
    }
    me();
    console.log(answer) //=> 'Abhisek'
  5. using let keyword
    explicit local variable declaration inside a function

    function person() {
     let age = 123;
    }

    or implicit global variable declaration when declared out of any function context

    let gender = 'male';
  6. assigning as a property to global object e.g. window object
    explicit global variable creation

    window.applicationName = 'TrimmerApp';
    console.log(applicationName); //=> 'TrimmerApp'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment