Javascript Interview questions
1) var vs let vs const A variable declared using ‘ var ’ can be redefined and even redeclared anywhere throughout its scope. var x = 30; console.log(x); //prints 30 x = "Hi"; //redefining or re-assigning (works without any error) console.log(x); //prints "Hi" var y = 10; console.log(y); //prints 10 var y = "Hello"; //Redeclaring (works without any error) console.log(y) //Prints "Hello" A variable declared using ‘ let ’ can be redefined within its scope but cannot be re-declared within its scope. let x = 11; console.log(x); //prints 11 x = "IB"; //works without any error console.log(x); //prints "IB" let y = 12; console.log(y); //prints 12 let y = "Scaler"; // error: Identifier y has already been declared let z = 13; if(true){ let z = "Fun"; //works without any error as scope is different. console.log(z) //prints "Fun" } console.log(z) //prints 13 A variable declared using ‘...