A lot of features given by ES6(ES2015). And good things are many developers use features for solving real-life problem-solving. But beginner and many developers forget to use and make complex programs or applications. Today we are going to cover the difference between let, var, and const.
Basically variable is based on scope, use, and hoisting. now let’s take deep dive into understand variables.
Let
Let variable is mostly replace for var declaration and solving variable scope issue. Let is preferable for variable declaration.
Let is block scope variable
Let variable is bounded between two {} curly braces. Everything inside the block is scoped for let variable.
So, Let variable declared inside the block and use only in a particular block. Let’s take a small example for more understanding.
let testLet = "say Hi";
if (true) {
let blockLet = "Javascript everywhere";
console.log(blockLet); // "Javascript everywhere"
}
console.log(blockLet) // blockLet is not defined
Based on above example, we see blockLet variable is undefined outside of if condition’s This variable has limit of two curly braces.
Let variable is good as compare to var variable and but some time we take care about scope of variable.
Let variable is updated as well but you can not declare again in same block. For example,
let test1 = 'Javascript Everywhere';
test1 = "Jsgrip for javascript developer";
let test2 = "Test more variable for re-declare";
let test2 = "Invalid variable"; // this is invalid declaration
Re-declared variable giver error like error: Identifier ‘test2’ has already been declared
Const
Variable declaration using const is maintain constant value. Const is similar as let variable.
Const variable is block scoped variable. Let and const is only access within block.
Const is not allow to update and re-declared.
Means Once you declare const variable then you can’t not update it’s value until block end. If you still try to update value then javascript gives error.
const jsConst = "Javascript Everywhere";
jsConst = "Javascript is good"; // error: Assignment to constant variable.
If you try below example that also gives a error
const jsConst = "Javascript everywhere";
const jsConst = "Javascript is good for developer";
// error: Identifier 'jsConst' has already been declared
Every const variable assign value at initialise or declaration time.
If you still try to declare variabe without value it gives an error like:
Uncaught SyntaxError: Missing initializer in const declaration
Var
Before ES6 update developer need to declare variable using var only. That’s why new way is necessary for declare variable.
Var is globally scope, function or locally. If you declare variable for program you can use anywhere in whole window.
If you declare variable inside function then you can use in whole function but you can’t access after that function.
Let’s take one example for batter understanding:
var testVar = "Javascipt Everywhere";
function myTestFun() {
var testHello = "Welcome to javascript blog";
}
Variable testVar is use globally in whole window is global scoped. variable testHello is function scoped you can’t access outside function.
Var is updated and re-declared as well. For Example
var testHello = "Javascript Everywhere";
var testHello = "Jsgrip is for developer";
Main problem with Var:
There is some weak point for var. Let’s take one example and understand issue.
var testJs = "Javascript everywhere";
if (true) {
var testJs = "Hello developer";
}
console.log(testJs) // "Hello developer"
When you don’t know variable already declared at that time you are override value and it’s not gives an error.
Maybe all developer are understand variable and it’s scoped. if you still any confusion then you can contact us we will explain again.