I have used a basic function βοΈ to show how variable declaration works in js.
Currently the code is on fire π₯ Try to run πββοΈ the below codes and correct βοΈ the errors β to save it from burning π.
The code is self explanatory. π³
1 π₯
function variables(){
console.log(`This will explain us if variables declared as 'const' are hoisted or not,
value of a = ${a}`);
const a ;// const declaration
console.log(`a after declaration but no value is initialized, can this be done ?
a = ${a}`);
a = 'aa'; // initialized const
console.log(`a after value is assigned, a = ${a}`);
console.log(`Hoisting of variables declared with 'var' keyword can be seen here,
value of b = ${b}`);
var b = 'bb' ; // initialize a with a value
console.log(`We can see here that b is no more undefined, values of b = ${b}`);
console.log(`This will explain us if variables declared as let are hoisted or not,
value of c = ${c}`);
let c ;
console.log(`c after declaration c = ${c}`);
c = 'cc';
console.log(`c after values initialized is c = ${c}`);
}
variables();
SyntaxError: missing = in const declaration
2 π₯
function variables(){
console.log(`This will explain us if variables declared as 'const' are hoisted or not,
value of a = ${a}`);
const a = 'aaa'; // const declaration
console.log(`a after declaration but no value is initialized, can this be done ?
a = ${a}`);
a = 'aa'; // initialized const
console.log(`a after value is assigned, a = ${a}`);
console.log(`Hoisting of variables declared with 'var' keyword can be seen here,
value of b = ${b}`);
var b = 'bb' ; // initialize a with a value
console.log(`We can see here that b is no more undefined, values of b = ${b}`);
console.log(`This will explain us if variables declared as let are hoisted or not,
value of c = ${c}`);
let c ;
console.log(`c after declaration c = ${c}`);
c = 'cc';
console.log(`c after values initialized is c = ${c}`);
}
variables();
ReferenceError: can't access lexical declaration 'a' before initialization
3 π₯
function variables(){
// console.log(`This will explain us if variables declared as 'const' are hoisted or not,
// value of a = ${a}`);
const a = 'aaa'; // const declaration
console.log(`a after declaration but no value is initialized, can this be done ?
a = ${a}`);
a = 'aa'; // initialized const
console.log(`a after value is assigned, a = ${a}`);
console.log(`Hoisting of variables declared with 'var' keyword can be seen here,
value of b = ${b}`);
var b = 'bb' ; // initialize a with a value
console.log(`We can see here that b is no more undefined, values of b = ${b}`);
console.log(`This will explain us if variables declared as let are hoisted or not,
value of c = ${c}`);
let c ;
console.log(`c after declaration c = ${c}`);
c = 'cc';
console.log(`c after values initialized is c = ${c}`);
}
variables();
TypeError: invalid assignment to const 'a'
4 π₯
function variables(){
// console.log(`This will explain us if variables declared as 'const' are hoisted or not,
// value of a = ${a}`);
const a = 'aaa'; // const declaration
//console.log(`a after declaration but no value is initialized, can this be done ?
// a = ${a}`);
//a = 'aa'; // initialized const
console.log(`a after value is assigned, a = ${a}`);
console.log(`Hoisting of variables declared with 'var' keyword can be seen here,
value of b = ${b}`);
var b = 'bb' ; // initialize a with a value
console.log(`We can see here that b is no more undefined, values of b = ${b}`);
console.log(`This will explain us if variables declared as let are hoisted or not,
value of c = ${c}`);
let c ;
console.log(`c after declaration c = ${c}`);
c = 'cc';
console.log(`c after values initialized is c = ${c}`);
}
variables();
ReferenceError: can't access lexical declaration 'c' before initialization
console log:
a after value is assigned, a = aaa
Hoisting of variables declared with 'var' keyword can be seen here,
value of b = undefined
We can see here that b is no more undefined, values of b = bb
5 π§―
function variables(){
// console.log(`This will explain us if variables declared as 'const' are hoisted or not,
// value of a = ${a}`);
const a = 'aaa'; // const declaration
//console.log(`a after declaration but no value is initialized, can this be done ?
// a = ${a}`);
//a = 'aa'; // initialized const
console.log(`a after value is assigned, a = ${a}`);
console.log(`Hoisting of variables declared with 'var' keyword can be seen here,
value of b = ${b}`);
var b = 'bb' ; // initialize a with a value
console.log(`We can see here that b is no more undefined, values of b = ${b}`);
//console.log(`This will explain us if variables declared as let are hoisted or not,
// value of c = ${c}`);
let c ;
console.log(`c after declaration c = ${c}`);
c = 'cc';
console.log(`c after values initialized is c = ${c}`);
}
variables();
no errors this time π
console log:
a after value is assigned, a = aaa
Hoisting of variables declared with 'var' keyword can be seen here,
value of b = undefined
We can see here that b is no more undefined, values of b = bb
c after declaration c = undefined
c after values initialized is c = cc
Finally !!!ππ we have seen π how to declare and initialize variables with var, let and const keywords in JS.
I would request the reader to comment out what all he learned from this post and if there are any mistakes please point them out as well with code sequence number. Thanks π
Top comments (0)