If you're new to learning JavaScript and have taken courses on youtube or anything other platform and don't really understand the difference between let and var or wondering why var isn't a standard in JavaScript anymore.
SAY NO MORE!
Let
first let's start with let. Let was introduced in ECMASCRIPT 6 === ES6 but you could use it a little bit in es5 for limited scope declarations. Wasn't until es6 that let and const were formerly introduced.
Stop using VAR!
Let is a great way to prevent bugs in your code. why you ask?
Because let is only limited to the scope of the block of code.
so lets say you have a function.
function user(){
let name = 'mike'
}
but you have another let name outside of the function.
let name = 'james';
remember all functions can access the global or root scope. so with let you cant reassign it, it can be modified but only inside the block scope.
Like this:
let = wizardLevel = false
function user(){
wizardLevel = true;
console.log('function',wizardLevel) <== returns true
}
user();
console.log(wizardLevel) <=== returns false
// because it happened outside of the function scope.
Look How var is accessed in if statment
var wizardLevel = false;
var experience = 100;
if(experience > 90){
wizardLevel = true;
console.log('inside',wizardLevel);
}
console.log('outside', wizardLevel);
Both wizardLevel variables will return true because of access to the scope. see image for results
Top comments (0)