JS

JavaScript Fundamentals

25 lessons

Progress0%
1. Introduction to JavaScript
1What is JavaScript?2Setting Up Your Environment
2. Variables and Data Types
1Declaring Variables2Data Types3Type Conversion
3. Operators
Arithmetic OperatorsComparison OperatorsLogical Operators
4. Control Flow
Conditional StatementsLoops
5. Functions
Function Basics
6. Arrays & Iteration
Array MethodsSpread, Rest & Destructuring
7. Objects & JSON
Working with ObjectsJSON & Optional Chaining
8. OOP & Classes
Class BasicsInheritance & Private Fields
9. Modules & Modern JS
ES ModulesModern JavaScript Features
10. Async JavaScript
PromisesAsync/Await
11. Error Handling
Error Types & try/catchCustom Errors & Debugging
12. Iterators & Advanced
Iterators & GeneratorsMap, Set & WeakRefs
All Tutorials
JavaScriptOperators
Lesson 7 of 25 min
Chapter 3 · Lesson 2

Comparison Operators

Comparison operators compare two values and return a boolean (true or false).

Equality Operators:

OperatorDescriptionExampleResult
==Equal (with type coercion)"5" == 5true
===Strict equal (no coercion)"5" === 5false
!=Not equal (with coercion)"5" != 5false
!==Strict not equal"5" !== 5true

Relational Operators:

OperatorDescriptionExampleResult
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 3false

Best Practice: Always use === and !== to avoid unexpected type coercion bugs.

String Comparison: Strings are compared character by character using Unicode values:

  • "apple" < "banana" (true - 'a' comes before 'b')
  • "2" > "10" (true - comparing first characters '2' vs '1')

Code Examples

Comparison Operationsjavascript
// Equality operators
console.log(5 == "5");   // true (type coercion)
console.log(5 === "5");  // false (strict - different types)
console.log(5 !== "5");  // true (strict not equal)

// null and undefined
console.log(null == undefined);   // true
console.log(null === undefined);  // false

// Relational operators
console.log(10 > 5);    // true
console.log(10 < 5);    // false
console.log(10 >= 10);  // true
console.log(10 <= 9);   // false

// String comparison (alphabetical)
console.log("apple" < "banana");  // true
console.log("Apple" < "apple");   // true (uppercase comes first)
console.log("10" < "9");          // true (string comparison!)
console.log(10 < 9);              // false (number comparison)

// Comparing different types
console.log("10" > 5);    // true (string converted to number)
console.log("ten" > 5);   // false (NaN is not > 5)

Always be careful when comparing different types. Use === for predictable behavior.

Quick Quiz

1. What is the result of null === undefined?

2. Why should you use === instead of ==?

Was this lesson helpful?

PreviousNext