JavaScript Variables Var Let Const

What is Temporal Dead Zone

** Plain English Variable Data Types and Immutability in JavaScript ** | Demo 2s JavaScript Variables | Declaration Scope | Demo 2s Let Declaration Scope



| | | |


    var a = 'a';
    console.log(a);
    
    let b = 'b';
    console.log(b);
    
    const c = 'c'
    console.log(c);

  


Decloration Scope


    function test() {
      var myValue = "hi";  // local variable
   }
   test();
   console.log(myValue); // error!

  


Global


  function test() {
  myValue = "hi";  // global variable
}
test();
console.log(myValue); // "hi"



Javascript let Declarations
Demo 2s JavaScript Let Declarations

let keyword works like var with some important differences.
let is !block scoped, but var is !function scoped.

** Stack Overflow **

    if (true) {
      var name = 'Javascript';
      console.log(name);  // Javascript
    }
    console.log(name);    // Javascript
    
    if (true) {
      let age = 11;
      console.log(age);   // 11
    }
    console.log(age);     // ReferenceError: age is not defined

  


  

| | | | |



      
    


JavaScript Variables let Block scope

Variables declared using the let keyword are block-scoped, which means that they are available only in the block in which they were declared.

At the top level (outside of a function)

At the top level, variables declared using let don't create properties on the global object.

Stack Overflow difference let and var | Let vs Var Code Sandbox | MDN Let | w3 let | JavaScript let: Declaring Block-Scoped Variables

      var globalVariable = 42;
let blockScopedVariable = 43;

console.log(globalVariable); // 42
console.log(blockScopedVariable); // 43

console.log(this.globalVariable); // 42
console.log(this.blockScopedVariable); // undefined
    
Ways to Create a Variable