Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 21
Q526 - Control Flow
The condition if (!a) can be replaced by the condition:
-
A
- if (a)
-
B
- if (a === false)
-
C
- if (a == false)
-
D
- if (!!a)
Reveal correct answer
Correct answer: C
Explanation
Topics: if equality operator strict equality operator
logical NOT operator
Try it yourself:
- console.log("!a:")
- console.log(Boolean(!1)); // false
- console.log(Boolean(!0)); // true
- console.log(Boolean(!"Hello")); // false
- console.log("a == false")
- console.log(Boolean(1 == false)); // false
- console.log(Boolean(0 == false)); // true
- console.log(Boolean("Hello" == false)); // false
- console.log("a:")
- console.log(Boolean(1)); // true
- console.log(Boolean(0)); // false
- console.log(Boolean("Hello")); // true
- console.log("!!a:")
- console.log(Boolean(!!1)); // true
- console.log(Boolean(!!0)); // false
- console.log(Boolean(!!"Hello")); // true
- console.log("a === false")
- console.log(Boolean(1 === false)); // false
- console.log(Boolean(0 === false)); // false
- console.log(Boolean("Hello" === false)); // false
Explanation:
!!a and a are the opposite of !a
and the strict equality operator is just too strict for this
and does for example not do the same thing with the number 0
The if statement executes a block of code if a specified condition is true
https://www.w3schools.com/jsref/jsref_if.asp
The equality operator checks whether its two operands are equal,
returning a Boolean result.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality
The strict equality operator checks whether its two operands are equal,
returning a Boolean result. Unlike the equality operator,
the strict equality operator always considers
operands of different types to be different.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality
The logical NOT operator takes truth to falsity and vice versa.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
(There is a similar question Q426.)
Q526 (Please refer to this number, if you want to write me about this question.)
A. The condition if (!a) checks if the value of 'a' is falsy, so directly using if (a) as a replacement would invert the logic and check if 'a' is truthy instead of falsy, which is not equivalent to the original condition.
B. The condition if (!a) checks if the value of 'a' is falsy, not strictly equal to false. Therefore, replacing it with the condition if (a === false) would only be true if 'a' is false and not for other falsy values like null, undefined, 0, or an empty string.
C. The condition if (!a) checks if the value of 'a' is falsy, meaning it is equivalent to false. Therefore, replacing it with the condition if (a == false) achieves the same result by explicitly comparing 'a' to false.
D. The condition if (!a) already checks if 'a' is falsy, so using if (!!a) as a replacement would double negate the value of 'a', resulting in checking if 'a' is truthy instead of falsy, which is not equivalent to the original condition.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
