25
loading...
This website collects cookies to deliver better user experience
var
: Globally or function scopedlet/const
: Block scopedvar
: Can be updated and redeclaredlet
: Can be updated but not redeclaredconst
: Can't be updated or redeclaredvar
: Default initialized as undefined
let/const
: not initialisedvar/let
: Can be declared without being initialisedconst
: Must be initialisedvar fuu = "I'm the var";
let bar = 'Let it go';
const fuubar = "I'm a const";
console.log(window.fuu); // I'm the var
console.log(window.bar); // undefined
console.log(window.fuubar); // Undefined
var fuu = 'Var value 1';
var fuu = 'Var value 2'; // Sure we'll reassign this
let bar = 'Let value 1';
let bar = 'Let value 2'; // Uhh, no! you declared this already
const fuubar = 'Const value 1';
const fuubar = 'Const value 2'; // Stop! You declared me already
var fuu = 'Var value 1';
fuu = 'Var value 2';
let bar = 'Let value 1';
bar = 'Let value 2';
const fuubar = 'Const value 1';
fuubar = 'Const value 2'; // TypeError! Assignment to const
console.log(fuu); // Var value 2
console.log(bar); // Let value 2
console.log(fuubar);
const fuu = {name: 'Chrizz'};
fuu.name = 'Chris';
console.log(fuu); // { name: 'Chris' }
const fuu = {name: 'Chrizz'};
fuu = {...fuu, ...{name: 'Chris'}};
console.log(fuu); // Undefined
var fuu = 'Var value';
console.log(fuu); // Var value
console.log(bar); // Hold on! Reference error
let bar = 'Let value';
console.log(bar);
var
: You no longer need this unless you need to support ancient browsers...let
: The variable must change a loop counter, a string that is dependant on an action.const
: This value should not change. I tend to default to const unless I realise it needs to change. And again, if it's an array or object, we can still use const.const
unless you want to reassign value use let
and don't use var
unless you require super old browser support.