Curriculum
Comparison Operators in JavaScript are used to compare two values and return either true or false. Understanding Comparison Operators is important for beginners because these operators are widely used in conditional statements, loops, validations, and decision-making systems in JavaScript programming and web development.
Comparison Operators help JavaScript programs compare values.
These operators are mainly used in:
Comparison Operators always return:
truefalseThese boolean results help applications make decisions.
Example:
console.log(10 > 5);
Output:
true
Because:
Comparison Operators are important because they help developers:
Almost every JavaScript application uses comparison operations.
Main Comparison Operators:
| Operator | Meaning |
|---|---|
== |
Equal to |
=== |
Strict equal to |
!= |
Not equal to |
!== |
Strict not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
Each operator performs a different comparison.
The == operator checks whether two values are equal.
Example:
console.log(10 == 10);
Output:
true
Example:
console.log(10 == "10");
Output:
true
Because:
== compares values onlyThe === operator compares:
Example:
console.log(10 === "10");
Output:
false
Because:
Strict equality is safer and preferred in modern JavaScript.
The != operator checks whether values are different.
Example:
console.log(10 != 5);
Output:
true
Because:
The !== operator compares both:
Example:
console.log(10 !== "10");
Output:
true
Because:
The > operator checks whether the left value is greater.
Example:
console.log(20 > 10);
Output:
true
This operator is commonly used in:
The < operator checks whether the left value is smaller.
Example:
console.log(5 < 10);
Output:
true
This operator is useful in validations and comparisons.
The >= operator checks:
Example:
console.log(18 >= 18);
Output:
true
This operator is often used in eligibility systems.
The <= operator checks:
Example:
console.log(15 <= 20);
Output:
true
This is commonly used in range validations.
Example:
let age = 22;
if(age >= 18){
console.log("Eligible to Vote");
}
Output:
Eligible to Vote
Comparison operators are essential for conditional logic.
Comparison Operators are used in:
They are a core part of application logic.
Example:
let password = prompt("Enter Password");
if(password === "admin123"){
console.log("Access Granted");
}
else{
console.log("Wrong Password");
}
This creates secure authentication logic.
Beginners often:
== and ===Incorrect example:
let age = 18;
if(age = 18){
console.log("Correct");
}
Correct example:
let age = 18;
if(age === 18){
console.log("Correct");
}
Understanding strict comparison prevents bugs.
Benefits include:
Comparison operators are essential for building interactive applications.
Best practices include:
=== over ==Professional developers prefer strict comparison operators for reliability.
Understanding Comparison Operators helps developers:
These operators are foundational in JavaScript programming.
Comparison Operators in JavaScript are used to compare values and return boolean results. Operators like ==, ===, >, <, and != help developers build conditions, validations, and logical systems in web applications and software development.
Comparison Operators compare two values and return true or false.
== compares only values, while === compares both values and data types.
They help developers build conditions, validations, and application logic.
They are used in login systems, forms, banking apps, and conditional statements.
The strict equality operator === is preferred for accurate comparisons.
WhatsApp us