Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
cls = 10; class cls {} console.log(typeof cls);
a.number
b.function
c.class cls{}
d.error
e.10
Answer:d
Reason: The error is - cannot access 'cls' before initialization.
Identify the output of the below program
function fx() { i=10; } fx(); console.log("value", i)
a.error
b.10
c.undefined
d.null
e.no output
Answer:b
Reason: The variable 'i' is module level and hence is accessible.
Identify the output of the below program
var j=9; if (j>10) i=35; console.log("value",i);
a.error
b.9
c.35
d.undefined
e.no output
Answer: a
Reason: The variable 'i' is not defined hence the error.
Identify the output of the below program
console.log(typeof console.log);
a.object
b.function
c.class
d.string
e.undefined
Answer:b
Reason: The typeof console.log is a function.
Identify the output of the below program
var j=11; if (j>10) i=35; console.log("value",i);
a.11
b.35
c.undefined
d.error
e.no-output
Answer:b
Reason: Since the condition is true, the variable 'i' is initialized. Since variable 'i' is module level can be accessed outside the 'if' block.
Identify the output of the below program
if (10 > (i=3)){ console.log("true"); } else { console.log("false"); }
a.true
b.false
c.error
d.undefined
e.no output
Answer: a
Reason: The condition is true hence 'true' is printed.
Identify the output of the below program
console.log(typeof console.log());
a.number
b.undefined
c.function
d.object
e.error
Answer: b
Reason: The call to console.log() returns nothing hence the result is undefined.