let question = () => {
{
let firstname = "anurag";
var surname = "nayak";
}
console.log(firstname);
console.log(surname);
};
question();
index.js:918 Uncaught ReferenceError: firstname is not defined
at question (index.js:918:15)
at index.js:922:1
let question1 = () => {
{
(function () {
let firstname = "anurag";
var surname = "nayak";
});
}
console.log(surname);
console.log(firstname);
};
question1();
Output
index.js:931 Uncaught ReferenceError: surname is not defined
at question1 (index.js:931:15)
at index.js:935:1
const guessOutput = () => {
console.log(employeeName);
console.log(employeeId);
var employeeName = "Anurag Nayak";
let employeeId = "123";
};
guessOutput();
Uncaught ReferenceError: Cannot access 'employeeId' before initialization
at guessOutput (index.js:1059:15)
at index.js:1065:1
Guess the output
let x = 1;
const guessOutput = () => {
let x = 4;
const inner = () => {
x++;
var x = 10;
console.log(x);
};
inner();
};
guessOutput();
//Guess the output
//output should be 10...rest all x are distractions
//Its related to closure
let vs var - Random question
for (let i = 0; i < 9; i++) {}
console.log(i); //Uncaught ReferenceError: i is not defined
for (var i = 0; i < 9; i++) {}
console.log(i); //9
var let const - redeclarable/updatable
key points
⦿ var is redeclarable and updatable.
⦿ let cannot be redeclared but updatable.
⦿ const is neither redeclarable nor updatable
//****************var************************
var test = "test";
var test = "test changed";
console.log(test);
output:- test changed
//****************let************************
let test = "test";
//below redeclaration is not allowed as let is already declared
let test = "can I redeclare ??";
//This is allowed as var can be re-declared...
const question1 = () => {
var x = "I am x";
if (true) {
var x = "I am getting modified...";
}
};
question1();
//you cannot do this...as let variable cant be re-declared...
const question2 = () => {
let x = "I am x";
if (true) {
var x = "I am getting modified...";
}
};
let vs var - Random question
{
let x = "Can I be accessed outside this block ??";
}
//Uncaught ReferenceError: x is not defined
//console.log(x);
{
var x = "Seems I can be accessed outside this block....";
}
//Seems I can be accessed outside this block....
console.log(x);