const, let and var in JavaScript
console.log("JavaScript tutorial: var, let and const");
var a = 45; //It is recommended to use "let" and "const" in place of "var"
var b = "Harry";
// b = 4
let b2 = "Harry2";
// let c = null //"let" can be updated but not re-declared
// let d = undefined
// const author = "Harry is author"
// let author = 5 //Throws an error because constant cannot be changed
// author = 10 //Throws an error because constant cannot be changed
//"const" can neither be updated nor be re-declared
//"const" must be initialized during declaration unlike let and var
// const bird; //Throws an error because "const" must be initialized during declaration
{
var b = "this"
console.log(b)
}
console.log(b)
{
let b2 = "this2" //"let" and "const" are block scoped
console.log(b2)
}
console.log(b2)
OUTPUT :
JavaScript tutorial: var, let and const
this
this
this2
Harry2
Comments
Post a Comment