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

JavaScript Comparison Operators

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

JavaScript Comparison Operators – FAQs

What is the difference between == and ===?

== compares values with type coercion, meaning it converts both values to a common type before comparing. === compares values without type coercion, ensuring both the value and type must match exactly.

When should I use == vs. ===?

Use === to avoid unexpected type coercion, ensuring both value and type are compared accurately. This makes your code more predictable and helps prevent potential bugs related to type conversion.

What is type coercion in JavaScript?

Type coercion is the automatic conversion of values to a common type during comparison. For example, in 5 == “5”, the string “5” is coerced to the number 5, resulting in true.

What are the differences between != and !==?

!= compares values with type coercion, converting both values to a common type before comparing. !== compares values without type coercion, ensuring both value and type differ, making it a stricter comparison.

How do the greater than and less than operators work?

> and < compare values directly to check if one is greater or less than the other, respectively. >= and <= compare values and also check for equality, ensuring one value is greater/less than or equal to the other.

What should I know about comparing different data types?

When comparing different data types, JavaScript may perform type coercion. To avoid unexpected results, use strict comparison operators (===, !==) and ensure the values being compared are of the same type before performing the comparison.



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 Date Comparison: How to Compare Dates in JavaScript?
JavaScript Date Comparison is a common task and used in many applications. Like - building scheduling features, filtering data by date, or just checking if one date is before or after another. However, working with dates can be tricky due to different formats and time zones. In this article, we will explore various ways to compare dates in JavaScri
4 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
Less Than(<) Comparison Operator in JavaScript
JavaScript Less Than(&lt;) Operator is used to compare two operands and return true if the left operand has a lesser value than the right operator. The values are converted to equal primitive types before conversion If both the values are strings the comparison is done on the basis of their Unicode. Boolean values like true and false are converted
1 min read
Less Than or Equal(<=) Comparison Operator in JavaScript
JavaScript Less Than or Equal(&lt;=) to the operator is used to compare two operands and return true if the left operand is smaller or equal to the right operand. The algorithm used for the comparison is the same as that of less than operator but equal condition is also checked Syntax: a&lt;=b Example 1: In this example, we will compare String, Num
1 min read
Greater Than or Equal(>=) Comparison Operator in JavaScript
JavaScript Greater Than or Equal Operator(&gt;=) is used to compare two operands and return true if the left operand has a value greater than or equal to the right operand. The algorithm used in the comparison is similar to that of less than comparison the only difference is that the values are swapped and equal values are checked before returning
1 min read
Comparison of Popular JavaScript ORMs: Prisma, TypeORM, and Sequelize
If you're unfamiliar with ORM, it's a powerful tool that allows us to translate database logic into simple JavaScript code. Throughout this article, we will discuss the pros and cons of each ORM and delve into specific use cases where one may be more suitable than the others. Prisma: Modern and LightweightPrisma is an ORM that has gained significan
4 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &amp;&amp;:(AND) Evaluates operands and retu
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
Comparison between GraphQL & RESTful Architecture
In this article, we will compare RESTful Architecture and GraphQL Architecture. GraphQL is new to the field and RESTful was performing well before the birth of GraphQL. REST: It is an acronym for Representational State Transfer. REST is an API design style architecture to create web services(APIs). Features of RESTful architecture: REST server impl
6 min read
AngularJS vs Angular2 | Features and Comparison
AngularJS is a front-end dynamic JavaScript framework, used for developing a 'Single Page Application'. AngularJS is managed by Google and is open-source. This means that it is open for contribution by developers around the globe. Due to this, there is a lot of material online available for free, to learn the technology. After its launch, AngularJS
4 min read
How to create Comparison accordion using Google AMP amp-accordion ?
Sometimes we have a lot of content to display and to make the website look pretty and short we make use of the collapsible text boxes. Collapsible text boxes are that division which is the combination of heading and content, generally only heading is visible but when it is hit the content is displayed. Setup: You have to import amp-accordion compon
3 min read
Comparison between Go & Node.js
Go: Go or Golang is a statically typed, open-source procedural-oriented programming language. It was designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson, and it was released on the 10th of November 2009. The language was designed using C programming language so the syntax of the language is quite similar to C language. However, Golan
5 min read
Create a Comparison Table with HTML and CSS
In this article, we will see how to create a comparison table using HTML and CSS. HTML is used to create the table structure and CSS adds styles to the table. The Comparison table contains different products and compares the products based on their features. Here, we have added courses as products and comparisons based on pricing, features, and rat
2 min read
Next.js vs Gatsby: A Comprehensive Comparison of React Frameworks in 2024
In the ever-changing world of front-end development, React.js stands out as a fundamental technology. Among the many frameworks that use React, Next.js and Gatsby often steal the spotlight, sparking lively debates among developers due to their unique strengths and practical applications. Both Next.js and Gatsby have earned their places as go-to ope
10 min read
How to do a Deep Comparison Between Two Objects using Lodash?
Deep Comparison between two objects is the process of thoroughly checking their properties and nested objects to determine if they are equal. Below are the possible approaches to do a deep comparison between two objects using Lodash.: Table of Content Using _.isEqual() FunctionUsing _.isEqualWith() Method with Comparator Function Using _.difference
4 min read
How to create Image Comparison Slider using CSS ?
In this article, we will see how to create an Image Comparison Slider using CSS. The Image Comparison Slider essentially aids in the distinction of two photographs or products. As a result, the user can quickly determine which of the two products or two images in a better way. Approach: The approach we are using for making this Image Comparison Sli
2 min read
How to compare two images using Image comparison slider?
In this project, we are going to create an image slider with which we can check 2 images. If we are making an exact copy of them. We can compare every single boundary of the first image with the second image in both the vertical and horizontal directions. Approach: Create an HTML file in which we are going to add the main div, further we are adding
3 min read
Spectre Comparison sliders
The Spectre Comparison slider is an image comparison slider that essentially aids in the distinction of two photographs or products. As a result, the user can quickly determine which of the two products or two images are better. In this article, we will create that with ease with the Spectre classes. Spectre Comparison sliders: comparison-slider: T
2 min read
Alternative to Multiple OR Operators in JavaScript
In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to perform the same task in other ways: C/C++ Code let u
4 min read
How to use multiple ternary operators in a single statement in JavaScript ?
In this article, we will see how we can use multiple ternary operators in a single statement in JavaScript. Sometimes using conditional statements like if, else, and else if stretches the code unnecessarily and makes the code even more lengthy. One trick to avoid them can be the use of ternary operators. But what if we are to use multiple if-else s
2 min read
Javascript Short Circuiting Operators
In JavaScript short-circuiting, an expression is evaluated from left to right until it is confirmed that the result of the remaining conditions is not going to affect the already evaluated result. If the result is clear even before the complete evaluation of the expression, it short circuits and the result will be returned. Short circuit evaluation
3 min read