Introduction
Namaste, In this blog I will discuss 10 code scenarios on double not operator.
Identify the output of the below program
let v = !!10; console.log(v);
a. true
b. false
c. 10
d. undefined
e. null
Answer: a
Reason: The double not operator for a non-zero number is true
Identify the output of the below program
let v = !!"JS"; console.log(v);
a.false
b.JS
c.undefined
d.null
e.true
Answer:e
Reason: The double not operator for a non-empty string is true
Identify the output of the below program
let v = !!" "; console.log(v);
a.true
b.false
c.undefined
d.null
e.error
Answer: a
Reason: The double not operator for a non-empty (even spaces) string is true
Identify the output of the below program
let v = !!""; console.log(v);
a.false
b.true
c.undefined
d.null
e.error
Answer: a
Reason: The double not operator for an empty string is false
Identify the output of the below program
let v = !!null; console.log(v);
a.null
b.undefined
c.true
d.false
e.error
Answer:d
Reason: The double not operator for null is false
Identify the output of the below program
let v = !!NaN; console.log(v);
a.undefined
b.NaN
c.true
d.false
e.error
Answer:d
Reason: The double not operator for NaN is false
Identify the output of the below program
let v = !!Infinity; console.log(v);
a.false
b.true
c.Infinity
d.null
e.error
Answer: b
Reason: The double not operator for Infinity is true
Identify the output of the below program
let v = !!-Infinity; console.log(v);
a.false
b.true
c.Infinity
d.null
e.error
Answer: b
Reason: The double not operator for -Infinity is true
Identify the output of the below program
let v = !!0; console.log(v);
a.false
b.true
c.Infinity
d.null
e.error
Answer: a
Reason: The double not operator for 0 is false
Identify the output of the below program
let v = !!{}; console.log(v);
a.false
b.true
c.Infinity
d.null
e.error
Answer: b
Reason: The double not operator for the empty object is true