Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
if (undefined) console.log("true"); else console.log("false");
a.true
b.false
c.error
d. undefined
e.NaN
Answer: b
Reason: Here undefined is a false value.
Identify the output of the below program
if(-1) console.log("true"); else console.log("false");
a.error
b.undefined
c.NaN
d. true
e.false
Answer: d
Reason: -1 is a truthy value.
Identify the output of the below program
if(null) console.log("true"); else console.log("false");
a.NaN
b.true
c.false
d. undefined
e.error
Answer: c
Reason: null is a falsey value.
Identify the output of the below program
if(2) console.log("true"); else console.log("false");
a.false
b.true
c. undefined
d. NaN
e.error
Answer: b
Reason: 2 is a truthy value.
Identify the output of the below program
if(0) console.log("true"); else console.log("false");
a.false
b.true
c.NaN
d. Error
e.undefined
Answer: a
Reason: 0 is a falsey value.
Identify the output of the below program
let str = "ram"; if(str) console.log("true"); else console.log("false");
a.false
b.NaN
c.undefined
d. true
e.error
Answer: d
Reason: A string is a truthy value.
Identify the output of the below program
if(NaN) console.log("true"); else console.log("false");
a.false
b.true
c.NaN
d. undefined
e.error
Answer: a
Reason: NaN is a falsey value.