Bird
Raised Fist0
Javascriptprogramming~20 mins

Comparison operators in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Master of Comparison Operators
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of chained comparison with mixed types
What is the output of this JavaScript code?
console.log(3 < 4 < 5);
Javascript
console.log(3 < 4 < 5);
ATypeError
Bundefined
Cfalse
Dtrue
Attempts:
2 left
๐Ÿ’ก Hint
Remember that JavaScript evaluates comparisons from left to right and converts booleans to numbers in some cases.
โ“ Predict Output
intermediate
2:00remaining
Output of strict equality with different types
What is the output of this JavaScript code?
console.log('5' === 5);
Javascript
console.log('5' === 5);
Atrue
Bfalse
CTypeError
Dundefined
Attempts:
2 left
๐Ÿ’ก Hint
Strict equality checks both value and type.
โ“ Predict Output
advanced
2:00remaining
Output of comparing objects with == and ===
What is the output of this JavaScript code?
const a = {};
const b = {};
console.log(a == b);
console.log(a === b);
Javascript
const a = {};
const b = {};
console.log(a == b);
console.log(a === b);
Afalse\ntrue
Btrue\ntrue
Cfalse\nfalse
Dtrue\nfalse
Attempts:
2 left
๐Ÿ’ก Hint
Objects are compared by reference, not by content.
โ“ Predict Output
advanced
2:00remaining
Output of comparing null and undefined with == and ===
What is the output of this JavaScript code?
console.log(null == undefined);
console.log(null === undefined);
Javascript
console.log(null == undefined);
console.log(null === undefined);
Atrue\nfalse
Bfalse\nfalse
Ctrue\ntrue
Dfalse\ntrue
Attempts:
2 left
๐Ÿ’ก Hint
== allows type coercion, === does not.
โ“ Predict Output
expert
3:00remaining
Output of comparing NaN with itself
What is the output of this JavaScript code?
console.log(NaN == NaN);
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
Javascript
console.log(NaN == NaN);
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
Afalse\nfalse\ntrue
Btrue\ntrue\ntrue
Ctrue\nfalse\nfalse
Dfalse\ntrue\nfalse
Attempts:
2 left
๐Ÿ’ก Hint
NaN is not equal to anything, even itself, except with Object.is.

Practice

(1/5)
1. Which comparison operator checks if two values are equal in both value and type in JavaScript?
easy
A. !==
B. ==
C. !=
D. ===

Solution

  1. Step 1: Understand equality operators

    == checks value equality but ignores type, while === checks both value and type.
  2. Step 2: Identify strict equality operator

    === is the strict equality operator that compares both value and type.
  3. Final Answer:

    === -> Option D
  4. Quick Check:

    Strict equality = === [OK]
Hint: Use === to check value and type equality [OK]
Common Mistakes:
  • Confusing == with ===
  • Using != instead of !==
  • Assuming == checks type
2. Which of the following is the correct syntax to check if variable a is NOT equal to variable b in both value and type?
easy
A. a !== b
B. a != b
C. a =! b
D. a !=== b

Solution

  1. Step 1: Identify the strict inequality operator

    !== checks if two values are not equal in value or type.
  2. Step 2: Check syntax correctness

    a !== b is correct syntax; others are either loose inequality or invalid.
  3. Final Answer:

    a !== b -> Option A
  4. Quick Check:

    Strict not equal = !== [OK]
Hint: Use !== for strict not equal check [OK]
Common Mistakes:
  • Using != which ignores type
  • Typing =! instead of !==
  • Using too many equals signs
3. What will be the output of the following code?
console.log(5 == '5');
console.log(5 === '5');
medium
A. true
true
B. true
false
C. false
true
D. false
false

Solution

  1. Step 1: Evaluate 5 == '5'

    Loose equality == converts types, so number 5 equals string '5', result is true.
  2. Step 2: Evaluate 5 === '5'

    Strict equality === checks type too; number 5 is not string '5', result is false.
  3. Final Answer:

    true
    false
    -> Option B
  4. Quick Check:

    == true, === false [OK]
Hint: == compares value only; === compares value and type [OK]
Common Mistakes:
  • Assuming both comparisons return true
  • Confusing == and === behavior
  • Ignoring type differences
4. Find the error in this code snippet:
if (a = 10) {
console.log('Equal');
} else {
console.log('Not equal');
}
medium
A. Variable a is not declared
B. Should use == instead of =
C. Should use === instead of =
D. No error, code is correct

Solution

  1. Step 1: Identify assignment vs comparison

    = assigns value 10 to a, not compares it.
  2. Step 2: Use strict equality for comparison

    Replace = with === to compare value and type correctly.
  3. Final Answer:

    Should use === instead of = -> Option C
  4. Quick Check:

    Use === for comparison, not = [OK]
Hint: Use === for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == or ===
  • Ignoring variable declaration
  • Assuming no error in if condition
5. You want to check if a variable score is between 50 and 100 inclusive. Which expression correctly uses comparison operators to do this?
hard
A. score >= 50 && score <= 100
B. 50 <= score <= 100
C. score > 50 || score < 100
D. score == 50 && score == 100

Solution

  1. Step 1: Understand range checking in JavaScript

    JavaScript does not support chained comparisons like 50 <= score <= 100 as in math.
  2. Step 2: Use logical AND with two comparisons

    Use score >= 50 && score <= 100 to check both conditions separately.
  3. Final Answer:

    score >= 50 && score <= 100 -> Option A
  4. Quick Check:

    Use && with two comparisons for range [OK]
Hint: Use && with two comparisons for ranges [OK]
Common Mistakes:
  • Using chained comparisons like 50 <= score <= 100
  • Using || instead of &&
  • Checking equality to both ends simultaneously
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard