Skip to content

Instantly share code, notes, and snippets.

@MidasXIV
Created September 4, 2019 07:10
Show Gist options
  • Save MidasXIV/c0382669aa2d048a736f5320fcefc44b to your computer and use it in GitHub Desktop.
Save MidasXIV/c0382669aa2d048a736f5320fcefc44b to your computer and use it in GitHub Desktop.
Var vs Let vs Const
// Hoisting
console.log(`var before decleration : ${v}`);
console.log(`let before decleration : ${l}`);
console.log(`const before decleration : ${c}`);
var v = 'VAR'
let l = 'LET'
const c = 'CONST'
// SCOPING
(() => {
if (true) {
var v = 'VAR';
let l = 'LET';
const c = 'CONST';
console.log(`var in block: ${typeof v !== 'undefined'? v:typeof v}`);
console.log(`let in block: ${typeof l !== 'undefined'? l:typeof l}`);
console.log(`const in block: ${typeof c !== 'undefined'? c:typeof c}`);
}
try {
console.log(`var in function: ${typeof v !== 'undefined' ? v:typeof v}`);
console.log(`let in function: ${typeof l !== 'undefined' ? l:typeof l}`);
console.log(`const in function: ${typeof c !== 'undefined' ? c:typeof c}`);
} catch (e) {
console.log(`${e.name} - ${e.message} in function scope`);
}
})();
try {
console.log(`var out of function: ${typeof v !== 'undefined'? v:typeof v}`);
console.log(`let out of function: ${typeof l !== 'undefined'? l:typeof l}`);
console.log(`const out of function: ${typeof c !== 'undefined'? c:typeof c}`);
} catch (e) {
console.log(`${e.name} - ${e.message} out of function scope`);
}
// Redeclaration
(() => {
if (true) {
var v = 'VAR';
let l = 'LET';
const c = 'CONST';
const v = 'VAR_re-declared';
const l = 'LET_re-declared';
const c = 'CONST_re-declared';
try {
console.log(`var in function: ${typeof v !== 'undefined' ? v:typeof v}`);
console.log(`let in function: ${typeof l !== 'undefined' ? l:typeof l}`);
console.log(`const in function: ${typeof c !== 'undefined' ? c:typeof c}`);
} catch (e) {
console.log(`${e.name} - ${e.message} in function scope`);
}
}
})();
// Reassignment
(() => {
if (true) {
var v = 'VAR';
let l = 'LET';
const c = 'CONST';
try {
v = 'VAR_UPDATED';
l = 'LET_UPDATED';
c = 'CONST_UPDATED'
} catch (e) {
console.log(`${e.name} - ${e.message}`);
}
console.log(`var update: ${v}`);
console.log(`let update: ${l}`);
console.log(`const update: ${c}`);
}
try {
var v2;
v2 = 10;
let l2;
l2 = 20;
// You must specify its value in the same statement in which its declared
const c2 = 30;
console.log(`var 2: ${++v2}`);
console.log(`let 2: ${++l2}`);
console.log(`const 2: ${++c2}`);
} catch (e) {
console.log(`${e.name} - ${e.message}`);
}
// Const with Arrays
const name = [];
name.push('pikachu');
name.push('milotic');
console.log(name)
const pokemon = {
name: 'Pikachu'
};
pokemon.name = 'Milotic' // this will work ! person variable is not completely reassigned, but mutated
console.log(pokemon.name) // "Milotic"
pokemon = "Kingler" // raises an error, because reassignment is not allowed with const declared variables
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment