The advantages of using let and const instead of var - 02

let: 

By using a let keyword, we can’t declare a variable twice but we can reassign a variable. 

let name = "mozahedul";
let name = "Islam";
console.log(name);

This code will generate an error like …


“Uncaught SyntaxError: Identifier 'name' has already been declared"

Reassigning the variable will not create any problem.


let name = "mozahedul"; name = "Islam"; console.log(name);

const: 

With a const keyword, we can only declare a variable and assign a value once only. We can’t reassign the value in the variable.


const name = "mozahedul"; name = "Islam"; console.log(name);

This code will generate the following error. 


“Uncaught TypeError: Assignment to constant variable."

 


Post a Comment

Previous Post Next Post