#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Comparison

Comparison operators compare two values and return a boolean, either true or false. You use them in if statements, loops, filters and anywhere your code must make a decision, such as "is the user over 18?" or "has the cart got any items?".

The full set of comparison operators

OperatorMeaningExampleResult
===Strictly equal (same type and value)5 === 5true
!==Strictly not equal5 !== "5"true
==Loosely equal (converts types first)5 == "5"true
!=Loosely not equal0 != ""false
> <Greater than, less than8 > 3true
>= <=Greater or equal, less or equal3 <= 3true

Strict versus loose equality

The double-equals operator converts both sides to a common type before comparing. That produces results that are hard to predict: 0 == "" is true, "1" == 1 is true, and null == undefined is true, yet null == 0 is false.

Triple-equals never converts. If the types differ, the answer is simply false. Use === and !== by default. Many teams allow one exception, value == null, as a short way to check for both null and undefined at once.

const age = Number(prompt('Your age?'));

if (age >= 18) {
  console.log('You can register.');
} else {
  console.log('Sorry, adults only.');
}

console.log(5 === '5');   // false, different types
console.log(5 == '5');    // true, string converted to number
console.log('10' < '9');  // true, compared as text!
console.log(10 < 9);      // false

Comparing strings

When both sides are strings, < and > compare character by character using their Unicode values. That is why "10" < "9" is true: the character "1" comes before "9". It is also why uppercase letters sort before lowercase ones, so "Zebra" < "apple" is true.

If you are comparing numbers that came from inputs, convert them first. For sorting words in a human-friendly way, use a.localeCompare(b), which handles case and language rules properly.

Traps with objects, arrays and NaN

  • Objects compare by reference. [1, 2] === [1, 2] is false, and so is {} === {}. Two separate objects are never equal, even with identical contents. Only two variables pointing to the very same object compare as equal.
  • NaN is not equal to itself. NaN === NaN is false. Use Number.isNaN(x) to test for it.
  • Chained comparisons do not work like maths. 1 < 5 < 3 evaluates 1 < 5 to true, then compares true < 3, which becomes 1 < 3, so the result is true. Write a < b && b < c instead.
  • Assignment instead of comparison. if (x = 5) assigns 5 and is always truthy. Linters catch this, which is one good reason to use one.

Using results directly

Because a comparison already produces a boolean, you can store it: const isAdult = age >= 18;. Writing if (isAdult === true) is unnecessary; if (isAdult) says the same thing more clearly. Named boolean variables like this make conditions much easier to read.

To remember: prefer ===, convert inputs to numbers before using < or >, and never expect two different objects to be equal.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course