The Wayback Machine - https://web.archive.org/web/20240622035653/https://www.geeksforgeeks.org/javascript-comparison-operators/
Open In App

JavaScript Comparison Operators

Last Updated : 09 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. They are used to evaluate whether a condition is true or false by comparing variables or values. These operators play a crucial role in logical expressions, helping to determine the equality or differences between values.

In this article, we will explore the various comparison operators available in JavaScript, understand how they work, and see examples of how to use them in different scenarios. By the end, you’ll have a solid grasp of how to utilize these operators to make your JavaScript code more dynamic and responsive.

Comparison Operators List

There are many comparison operators as shown in the table with the description.

OPERATOR NAME

USAGE

OPERATION

Equality Operatora == bCompares the equality of two operators
Inequality Operatora != bCompares inequality of two operators
Strict Equality Operatora === bCompares both the value and type of the operand
Strict Inequality Operatora !== bCompares inequality with type
Greater than Operatora > bChecks if the left operator is greater than the right operator
Greater than or equal Operator a >= bChecks if the left operator is greater than or equal to the right operator
Less than Operatora < bChecks if the left operator is smaller than the right operator
Less than or equal Operatora <= bChecks if the left operator is smaller than or equal to the right operator

1. Equality Operator (==)

The Equality operator is used to compare the equality of two operands. If equal then the condition is true otherwise false.

Example: Below example illustrates the (==) operator in JavaScript.

Javascript
// Illustration of (==) operator
let val1 = 5;
let val2 = '5';

// Checking of operands
console.log(val1 == 5);
console.log(val2 == 5);        
console.log(val1 == val1);

// Check against null and boolean value
console.log(0 == false);   
console.log(0 == null);

Output
true
true
true
true
false

2. Inequality Operator (!=)

The Inequality Operator is used to compare the inequality of two operands. If equal then the condition is false otherwise true.

Example: Below examples illustrate the (!=) operator in JavaScript.

Javascript
// Illustration of (!=) operator
let val1 = 5;
let val2 = '5';

// Checking of operands
console.log(val1 != 6);
console.log(val2 != '5');        
console.log(val1 != val2);

// Check against null and boolean value
console.log(0 != false);   
console.log(0 != null);

Output
true
false
false
false
true

3. Strict equality Operator (===)

The Strict equality Operator is used to compare the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.

Example: Below examples illustrate the (===) operator in JavaScript.

Javascript
// Illustration of (===) operator
let val1 = 5;
let val2 = '5';

// Checking of operands
console.log(val1 === 6);
console.log(val2 === '5');        
console.log(val1 === val2);

// Check against null and boolean value
console.log(0 === false);   
console.log(0 === null);

Output
false
true
false
false
false

4. Strict inequality Operator (!==)

The Strict inequality Operator is used to compare the inequality of two operands with type. If both value and type are not equal then the condition is true otherwise false.

Example: Below examples illustrate the (!==) operator in JavaScript.

Javascript
// Illustration of (!==) operator
let val1 = 5;
let val2 = '5';

// Checking of operands
console.log(val1 !== 6);
console.log(val2 !== '5');        
console.log(val1 !== val2);

// Check against null and boolean value
console.log(0 !== false);   
console.log(0 !== null);

Output
true
false
true
true
true

5. Greater than Operator (>)

The Greater than Operator is used to check whether the left-side value is greater than the right-side value. If the value is greater then the condition is true otherwise false.

Example: Below examples illustrate the (>) operator in JavaScript.

Javascript
// Illustration of (>) operator
let val1 = 5;
let val2 = "5";

// Checking of operands
console.log(val1 > 0);
console.log(val2 > "10");        
console.log(val1 > "10");
console.log(val2 > 0);

Output
true
true
false
true

6. Greater than or equal Operator (>=)

The Greater than or equal Operator is used to check whether the left side operand is greater than or equal to the right side operand. If the value is greater than or equal then the condition is true otherwise false.

Example: Below examples illustrate the (>=) operator in JavaScript.

Javascript
// Illustration of (>=) operator
let val1 = 5;
let val2 = "5";

// Checking of operands
console.log(val1 >= 5);
console.log(val2 >= "15");        
console.log(val1 >= "5");
console.log(val2 >= 15);

Output
true
true
true
false

7. Less than Operator (<)

The Less than Operator is used to check whether the left-side value is less than the right-side value. If yes then the condition is true otherwise false.

Example: Below examples illustrate the (<) operator in JavaScript.

Javascript
// Illustration of (<) operator
let val1 = 5;
let val2 = "5";

// Checking of operands
console.log(val1 < 15);
console.log(val2 < "0");        
console.log(val1 < "0");
console.log(val2 < 15);

Output
true
false
false
true

8. Less than or equal Operator (<=)

The Less than or equal Operator is used to check whether the left side operand value is less than or equal to the right side operand value. If yes then the condition is true otherwise false.

Example: Below examples illustrate the (<=) operator in JavaScript.

Javascript
// Illustration of (<=) operator
let val1 = 5;
let val2 = "5";

// Checking of operands
console.log(val1 <= 15);
console.log(val2 <= "0");        
console.log(val1 <= "0");
console.log(val2 <= 15);

Output
true
false
false
true

Supported Browsers: The browsers supported by all JavaScript Comparison operators are listed below:

We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Previous Article
Next Article

Similar Reads

What are Comparison Operators in JavaScript ?
Comparison operators in JavaScript are used to compare two values and return a boolean result (true or false). They include operators such as ==, ===, !=, !==, &gt;, &lt;, &gt;=, and &lt;=. Example: Here, the &gt; (greater than) the operator compares the values of a and b. Since a is not greater than b, the expression a &gt; b evaluates to false, w
1 min read
Does JavaScript Operators like or ( || ), and ( && ), null coalescing ( ?? ) makes Weak Comparison?
JavaScript operators like logical OR (||), logical AND (&amp;&amp;), and null coalescing (??) are used for making conditional evaluations and performing logical operations in JavaScript. While these operators are commonly used for controlling the flow of the code execution and handling values in the different scenarios it's essential to understand
2 min read
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Equal Operator == The comparison operator called Equal Operator is the double equal sign "==". This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same. This should always be kept in mind that the
2 min read
JavaScript ‘===’ vs ‘==’Comparison Operator
In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the javascript provides, though they look similar, they
3 min read
Comparison of Dart and JavaScript
In this article, we will compare Dart vs JavaScript, the two competing programming languages in cross-platform mobile application development. JavaScript reached the height of its popularity when it entered the fields of cross-platform mobile application development and server-side development. Since the Node.js framework can be used for both front
5 min read
Strict Equality(===) Comparison Operator in JavaScript
JavaScript Strict Equality Operator is used to compare two operands and return true if both the value and type of operands are the same. Since type conversion is not done, so even if the value stored in operands is the same but their type is different the operation will return false. Syntax: a===b Example 1: In this example, we will compare the val
2 min read
Inequality(!=) Comparison Operator in JavaScript
JavaScript Inequality operator is used to compare two operands and returns true if both the operands are unequal it is basically the opposite of the Equality Operator. It is also called a loose inequality check as the operator performs a type conversion when comparing the elements. Also in the case of object comparison, it only compares the referen
2 min read
Equality(==) Comparison Operator in JavaScript
In JavaScript, the Comparison Equality Operator is used to compare the values of the operand. The comparison operator returns true only if the value of two operands are equal otherwise it returns false. It is also called a loose equality check as the operator performs a type conversion when comparing the elements. When the comparison of objects is
2 min read
Strict Inequality(!==) Comparison Operator in JavaScript
JavaScript Strict Inequality Operator is used to compare two operators and return true if they both are unequal. It is the opposite of the Strict Equality operator but like the Strict Equality Operator, it also does not perform type conversion. Syntax: a!==b Example 1: In this example, we will use the inequality operator on the same data types. C/C
1 min read
Greater than(>) Comparison Operator in JavaScript
JavaScript Greater Than(&gt;) Operator is used to compare two operands and return true if the left operand has a higher value than the right operator. Syntax: a&gt;bExample 1: In this example, we will compare String, Number, and Boolean using Greater Than Operator. C/C++ Code console.log(&quot;3&quot;&gt;2); console.log(&quot;2&quot;&gt;3); console
1 min read