JavaScript not equal and Comparison Operators Explained

In this tutorial, you will learn about JavaScript not equal to operator, and the other Comparison operators along with examples.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting c…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this tutorial, you will learn about JavaScript not equal to operator, and the other Comparison operators along with examples.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - JavaScript Not Equal:

What are Comparison Operators in JS?

Comparison operators in programming languages are used to compare two values. These operators return a boolean value (true or false) based on the condition. Hence these operators are used in decision making or as conditional statements for loops.

Given its vast usage, every developer should understand the functionality of each operator. This article is a good starting point for the same, however, we do emphasize more on the JavaScript not equal (!= & !==) operators.

What is “!=” in JS?

The JavaScript not equal or inequality operator (!=) checks whether two values are not equal and returns a boolean value. This operator tries to compare values irrespective of whether they are of different types.

However, the “!==” or Strict inequality operator does not attempt to do so and returns false if the values are unequal or of different types.

Both these operators solve different purposes and hence I would recommend practicing them to facilitate further understanding.

Code and Explanation:

console.log(5 != 10);
// expected output: true

console.log(10 != 10);
// expected output: false

console.log(10 != '10');
// expected output: false

console.log(10 !== '10');
// expected output: true

In the first case, it returned true as the values were different. In the second and third cases, it returned a false cause the values are the same. Do note that in the latter case even though we passed 10 as a string the operator was able to compare both the values.

In the last case, we used the strict inequality operator and it returned true as the values were of different types.

Other Comparison Operators:

Apart from the JavaScript not equal and Strict inequality operators, we have a few other operators that solve different use cases. We have added a brief about them below.

  • Equal to (==) - Check if two values are equal
  • Strict equal to (===) - Checks is two values are equal and of similar type
  • Greater than (>) - Checks if the value on the left is greater than the value on the right
  • Greater than or equal to (>=) - Checks if the value is greater than or equal to the value on the right
  • Less than (<) - Checks if the value on the left is less than the value on the right
  • Less than or equal to (<=) - Checks if the value is less than or equal to the value on the right

Code and Explanation:

console.log(5 == 10);
// expected output: false
console.log(10 === 10);
// expected output: true
console.log(10 ==='10');
// expected output: false

console.log(5 > 10);
// expected output: false
console.log(5 >= 5);
// expected output: false

console.log(5 < 10);
// expected output: true
console.log(5 <= 5);
// expected output: true

Closing thoughts - JavaScript not equal:

In this tutorial, we covered the JavaScript not equal and the other comparison operators. As a next step do spend some quality time practicing the operators understand how they differ from each other and also try breaking them.

Once you are done with comparison operators do have a look at logical operators.


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-10-13T11:37:47+00:00) JavaScript not equal and Comparison Operators Explained. Retrieved from https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/

MLA
" » JavaScript not equal and Comparison Operators Explained." hrishikesh1990 | Sciencx - Wednesday October 13, 2021, https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/
HARVARD
hrishikesh1990 | Sciencx Wednesday October 13, 2021 » JavaScript not equal and Comparison Operators Explained., viewed ,<https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/>
VANCOUVER
hrishikesh1990 | Sciencx - » JavaScript not equal and Comparison Operators Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/
CHICAGO
" » JavaScript not equal and Comparison Operators Explained." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/
IEEE
" » JavaScript not equal and Comparison Operators Explained." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/. [Accessed: ]
rf:citation
» JavaScript not equal and Comparison Operators Explained | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/10/13/javascript-not-equal-and-comparison-operators-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.