Skip to content
the poor coder

JavaScript Comparison Operators

Poor Man
Published
Reading time
2 min read
On this page

JavaScript Comparison Operators#

JavaScript comparison operators are used to compare values and return a boolean value (true or false) based on the comparison result. There are various types of comparison operators in JavaScript, such as:

Equal operator (==)#

The equal operator compares two values for equality. It returns true if the operands are equal, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x == y;
      // result is false
    

Strict equal operator (===)#

The strict equal operator compares two values for equality without type coercion. It returns true if the operands are equal and of the same type, or false otherwise.

JavaScript

      var x = 10;
      var y = "10";
      var result = x === y;
      // result is false
    

Not equal operator (!=)#

The not equal operator compares two values for inequality. It returns true if the operands are not equal, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x != y;
      // result is true
    

Strict not equal operator (!==)#

The strict not equal operator compares two values for inequality without type coercion. It returns true if the operands are not equal or of different types, or false otherwise.

JavaScript

      var x = 10;
      var y = "10";
      var result = x !== y;
      // result is true
    

Greater than operator (>)#

The greater than operator compares two values and returns true if the left operand is greater than the right operand, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x > y;
      // result is true
    

Less than operator (<)#

The less than operator compares two values and returns true if the left operand is less than the right operand, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x < y;
      // result is false
    

Greater than or equal to operator (>=)#

The greater than or equal to operator compares two values and returns true if the left operand is greater than or equal to the right operand, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x >= y;
      // result is true
    

Less than or equal to operator (<=)#

The less than or equal to operator compares two values and returns true if the left operand is less than or equal to the right operand, or false otherwise.

JavaScript

      var x = 10;
      var y = 5;
      var result = x <= y;
      // result is false
    

These are the basic comparison operators used in JavaScript. You can use them to compare any two values in your code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC