Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
let mydata= -123.123; if (mydata) console.log("true"); else console.log("false");
a.false
b.true
c.undefined
d. null
e.error
Answer: b
Reason: A non-zero number is truthy.
Identify the output of the below program
function fx() { return; } if (fx()) console.log("true"); else console.log("false");
a.false
b.true
c.error
d. undefined
e.null
Answer: a
Reason: The function returns undefined as a value which is a falsey value.
Identify the output of the below program
function fx3() { let i = 0/10; return i; } if (fx3()) console.log("true"); else console.log("false");
a.error
b.undefined
c.false
d. true
e.null
Answer: c
Reason: The function returns 0 which is a falsey value.
Identify the output of the below program
let j = Infinity/Infinity; console.log("value", j);
a.Infinity
b.null
c.1
d. NaN
e.error
Answer: d
Reason: The Infinity/Infinity yields into NaN in Javascript.
Identify the output of the below program
if ("") console.log("true"); else console.log("false");
a.error
b.undefined
c.error
d. false
e.true
Answer: d
Reason: Empty string is a falsey value.
Identify the output of the below program
function fx4() { let i = Infinity/Infinity; return i; } if (fx4()) console.log("true"); else console.log("false");
a.true
b.false
c.error
d. NaN
e.undefined
Answer:b
Reason: The function returns NaN which is a falsey value.
Identify the output of the below program
function fx2() { let i = 1/0; return i; } if (fx2()) console.log("true"); else console.log("false");
a.false
b.true
c.undefined
d. null
e.error
Answer:b
Reason: The function returns Infinity which is a truthy value.