Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
const i=10; let j=20; const i=30; console.log("value", i);
a.30
b.10
c.20
d. error
e.undefined
Answer: d
Reason: The 'const' variable cannot be re-declared.
Identify the output of the below program
let k=20; if (k>10) { const a=4; } console.log("value", a);
a.20
b.4
c.undefined
d. null
e.error
Answer: e
Reason: The 'const' has a block scope and hence cannot be accessed outside the block.
Identify the output of the below program
const obj = { empno:10, empname:"ram" }; obj.empno = 50; console.log("value", obj);
a.error
b.{empno:10, empname:"ram"}
c.{empno:50, empname:"ram"}
d. undefined
e.{empno:50}
Answer: c
Reason: The obj is 'const', not it's properties.
Identify the output of the below program
function fx() { const i=10; function fx2() { console.log("value", i); } fx2(); } fx();
a.10
b.error
c.undefined
d. null
e.0
Answer: a
Reason: In the given context the variable i is global to fx2 function hence accessible inside it.
Identify the output of the below program
try { const i=2; }catch(e) { console.log("error",e); } console.log("value",i);
a. 2
b.error
c.0
d. undefined
e.null
Answer: b
Reason: The 'const' has a block scope.
Identify the output of the below program
console.log("value", a); const a=10;
a.10
b.undefined
c.error
d. null
e.0
Answer: c
Reason: The 'const' is not hoisted hence the error.
Identify the output of the below program
const obj2 = function () {} obj2.i = 30; console.log("value", obj2.i);
a.function(){}
b.30
c.error
d. undefined
e.null
Answer: b
Reason: The object obj2 is const, not its properties.