Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
if (null+true) console.log("true"); else console.log("false");
a.false
b.true
c.error
d. undefined
e.null
Answer: b
Reason: Here null is coerced to 0 and true to 1 hence 0+1 is 1 which is truthy.
Identify the output of the below program
if ([1,2,3]) console.log("true"); else console.log("false");
a.false
b.true
c.error
d. undefined
e.null
Answer: b
Reason: An array object is a truthy value.
Identify the output of the below program
function fx() {} if (fx) console.log("true"); else console.log("false");
a.null
b.error
c.false
d. undefined
e.true
Answer: e
Reason: A function is an object and an object is a truthy value.
Identify the output of the below program
function fx(){} if (new fx()) console.log("true"); else console.log("false");
a.error
b.null
c.undefined
d. false
e.true
Answer: e
Reason: An object is a truthy value.
Identify the output of the below program
if (new Date()) console.log("true"); else console.log("false");
a.undefined
b.error
c.false
d. true
e.date
Answer: d
Reason: An object is a truthy value.
Identify the output of the below program
function fx() {} if (fx()) console.log("true"); else console.log("false");
a.error
b.false
c.true
d. undefined
e.0
Answer: b
Reason: The function call returns undefined which is falsey.
Identify the output of the below program
if ({name:"Ram"}) console.log("true"); else console.log("false");
a.undefined
b.null
c.true
d. false
e.error
Answer: c
Reason: An object is a truthy value.