0
0
Javascriptprogramming~5 mins

Comparison operators in Javascript

Choose your learning style9 modes available
Introduction

Comparison operators help us check if things are equal, bigger, or smaller. This helps the computer decide what to do next.

Checking if a user entered the right password
Deciding if a number is bigger than another to show a message
Finding out if two values are the same before continuing
Sorting items by comparing their sizes or names
Syntax
Javascript
value1 == value2
value1 === value2
value1 != value2
value1 !== value2
value1 > value2
value1 >= value2
value1 < value2
value1 <= value2

Use === and !== for exact matches including type.

Use == and != for loose comparison (not recommended).

Examples
== checks value only, so 5 and '5' are equal. === checks value and type, so they are not equal.
Javascript
5 == '5'  // true
5 === '5' // false
Check if one number is bigger or smaller or equal to another.
Javascript
10 > 7  // true
3 <= 3  // true
Check if two strings are not exactly the same.
Javascript
'apple' !== 'orange'  // true
Sample Program

This program compares numbers and strings using different comparison operators and prints the results.

Javascript
const a = 10;
const b = '10';

console.log(a == b);   // true
console.log(a === b);  // false
console.log(a > 5);    // true
console.log(a < 5);    // false
console.log(a != b);   // false
console.log(a !== b);  // true
OutputSuccess
Important Notes

Always prefer === and !== to avoid unexpected results.

Comparison operators return true or false, which are useful in decisions.

Summary

Comparison operators check how two values relate to each other.

=== and !== check both value and type.

They help the program decide what to do next.