📊 JavaScript
Q. The snippet that has to be used to check if “a” is not equal to “null” is _________
  • (A) if(a!==null)
  • (B) if(a!null)
  • (C) if (!a)
  • (D) if(a!=null)
💬 Discuss
✅ Correct Answer: (A) if(a!==null)

Explanation:

In JavaScript, we use !== (strict inequality operator) to check both:

  1. Value inequality
  2. 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).

Explanation by: Mr. Dubey

In JavaScript, we use !== (strict inequality operator) to check both:

  1. Value inequality
  2. 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).

💬 Discussion


📊 Question Analytics

👁️
208
Total Visits
📽️
4 y ago
Published
🎖️
Kirti
Publisher
📈
82%
Success Rate