Q. The snippet that has to be used to check if “a” is not equal to “null” is _________
✅ Correct Answer: (A)
if(a!==null)
Explanation:
In JavaScript, we use !== (strict inequality operator) to check both:
- Value inequality
- Type inequality
Example: Checking if a is not null
var a = "Hello"; // Example value
if (a !== null) {
console.log("a is not null");
} else {
console.log("a is null");
}
Output:
a is not null
If a = null, then the condition fails.
Why Not Other Options?
- (B) if(a!null) → ❌ Incorrect (invalid syntax; ! should be used with boolean expressions, not values).
- (C) if(!a) → ❌ Incorrect (this checks if a is falsy, but 0, "", undefined, NaN, and false are also falsy, not just null).
- (D) if(a != null) → ✅ Correct, but (A) is preferred because != does type coercion, whereas !== strictly checks for null.
Final Answer:
✅ (A) if(a !== null) (Best practice for checking null).