- JS Introduction
- JS Introduction
- JS Comments
- JS Variables
- JS Datatypes
- JS Operators
- JS Type Conversions
- JS Control Flow
- JS Comparisons
- JS If else
- JS If else Ladder
- JS Ternary Operator
- JS Switch
- JS For Loop
- JS For In
- JS For Of
- JS While
- JS Do While
- JS Break & Continue
- JS Functions
- JS Function Declaration
- JS Function Parameters
- JS Return Statement
- JS Function Expressions
- JS Anonymous Functions
- JS Objects
- JS Objects
- JS Object Methods
- JS Object Constructors
- JS Object Destructuring
- JS Object Prototypes
- JS Map, Filter & Reduce
- JS ES6
- JS ES6
- JS let and const
- JS Arrow Functions
- JS Template Literals
- Destructuring Assignment
- JS Spread Operator
- JS Default Parameters
- JS Classes
- JS Inheritance
- JS Map
- JS Set
- JS Async
- JS Callbacks
- JS Asynchronous
- JS Promises
- JS Async/Await
- JS HTML DOM/BOM
- JS Document Object
- JS getElementbyId
- getElementsByClassName
- JS getElementsByName
- getElementsByTagName
- JS innerHTML
- JS outerHTML
- JS Window Object
- JS History Object
- JS Navigator Object
- JS Screen Object
JavaScript Comparisons
In JavaScript, comparisons are the foundation of logic. We use comparison operators to evaluate conditions—essentially asking the computer questions that can be answered with a true or false (Boolean). Whether you are validating a user's password, checking if a shopping cart is empty, or filtering a list of products, you will rely on these operators to drive your application's decision-making process.
if...else blocks or while loops to control the flow of your program.
Here are the main comparison operators you need to master:
Equal to (==) and Strict Equal to (===)
Equal to (==): Also known as "loose equality." It checks if two values are equal after attempting to convert them to a common type. This process is called type coercion. For example, it will treat the string "5" and the number 5 as the same value.
Strict Equal to (===): This is the gold standard for comparisons. It checks both the value and the data type. If the types are different (like a string and a number), it immediately returns false without trying to convert them.
// Loose equality: Coerces the string to a number
console.log(5 == '5'); // true
// Strict equality: Types don't match (number vs string)
console.log(5 === '5'); // false
// Real-world example: Checking a numeric ID from a URL (which is usually a string)
let userIdFromUrl = "101";
if (userIdFromUrl == 101) {
console.log("User found!"); // This works, but can be risky
}
=== (strict equality). It makes your code more predictable and prevents subtle bugs caused by JavaScript's automatic type conversion. If you need to compare different types, convert them manually first.
= for comparison. Remember: = is for assignment (setting a value), while == or === is for comparison (checking a value).
Not Equal to (!=) and Strict Not Equal to (!==)
Not Equal to (!=): Checks if two values are not equal, performing type coercion if necessary. It returns true if the values are different and false if they are the same.
Strict Not Equal to (!==): Checks if two values are not equal or if their types are different. This is the inverse of === and is the safer way to check for inequality.
console.log(5 != '5'); // false (because they are coerced to be equal)
console.log(5 !== '5'); // true (because the types are different)
// Practical example: Ensuring a user is NOT an admin
let userRole = "editor";
if (userRole !== "admin") {
console.log("Access restricted to administrative settings.");
}
Greater Than (>) and Less Than (<)
Greater Than (>): Evaluates if the value on the left is strictly larger than the value on the right.
Less Than (<): Evaluates if the value on the left is strictly smaller than the value on the right.
console.log(10 > 5); // true
console.log(5 < 10); // true
// Real-world example: Age verification
let userAge = 16;
if (userAge < 18) {
console.log("Minor accounts require parental consent.");
}
> or <, JavaScript compares them alphabetically (lexicographically). For example, "apple" < "banana" is true, but "2" > "12" is also true because "2" comes after "1" in character ordering.
Greater Than or Equal to (>=) and Less Than or Equal to (<=)
Greater Than or Equal to (>=): Checks if the left value is either larger than or exactly equal to the right value.
Less Than or Equal to (<=): Checks if the left value is either smaller than or exactly equal to the right value.
console.log(10 >= 10); // true
console.log(15 >= 10); // true
console.log(5 <= 10); // true
// Real-world example: Inventory management
let stockCount = 5;
let minimumRequired = 5;
if (stockCount <= minimumRequired) {
console.log("Reorder triggered: Stock is at or below minimum levels.");
}
Comparison operators are simple tools, but they are incredibly powerful. They return boolean values (true or false) that act as the binary "on/off" switches for your application logic. By using strict comparisons (=== and !==), you ensure your code remains robust and free of unexpected type-related errors.