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"
let keyword works like var with some important differences.
let is !block scoped, but var is !function scoped.
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
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, 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