Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
if (NaN+1) console.log("true"); else console.log("false");
a.false
b.true
c.error
d. undefined
e.NaN
Answer: a
Reason: NaN+1 is NaN which is a falsey value hence result is false.
Identify the output of the below program
if (-Infinity) console.log("true"); else console.log("false");
a.error
b.undefined
c.true
d. false
e.-Infinity
Answer:c
Reason: -Infinity is a truthy value.
Identify the output of the below program
if(null+1) console.log("true"); else console.log("false");
a.error
b.NaN
c.false
d. true
e.undefined
Answer: d
Reason: The null is coerced into a number which is 0 hence (0 + 1) is 1, which is a truthy value.
Identify the output of the below program
if ({}) console.log("true"); else console.log("false");
a.undefined
b.null
c.true
d.error
e.false
Answer: c
Reason: An empty object is a truthy value.
Identify the output of the below program
if(undefined+1) console.log("true"); else console.log("false");
a.true
b.false
c.error
d. undefined
e.1
Answer: b
Reason: The undefined is coerced into a number which is NaN and NaN + 1 is NaN which is falsey.
Identify the output of the below program
if (Infinity) console.log("true"); else console.log("false");
a.true
b.false
c.error
d.undefined
e.Infinity
Answer: a
Reason: Infinity is a number that is non-zero and it is considered to be a truthy value.
Identify the output of the below program
if ([]) console.log("true"); else console.log("false");
a. true
b.error
c.false
d. undefined
e.0
Answer: a
Reason: An empty array is an object that is a truthy value.