Loading...

Comparisons in JavaScript

JavaScript has many ways to compare data. There are both strict and pliant comparisons avaialable

Strict comparisons require the type of the data as well as the values be identical. For this, a number can only match a number and they must have the same value, a string only matches another string with the same value, etc.

The more pliant comparisons check for a match to the data value, and does not care about the type. Technically, The two typed are converted to match before the comparison is made, so it does address a difference in type, but this is not considered during the comparison

The folowing show the operators used to make comparisons.

  • === Equal in type and value.
  • !== Not equal in type and/or value.
  • >= Greater than or equal to.
  • <= Less than or equal to.
  • > Greater than.
  • < Less than.

↓ Examples ↓

3 === '3' //    RESULT: false because === compares type and one is a number while the other is a string

3 !== '3' //    RESULT: true because !== compares if these are NOT the same type and value

3 != '3' //    RESULT: false because != compares if these are NOT the same value, but they are. This does not check the type

3 >= 3 //    RESULT: true because this test if the first value is either greater-than or equal-to

7 <= 3 //    RESULT: false, because 7 is neither less than or equal to 3

7 > 3 //    RESULT: true because 7 is greater than 3

7 < 3 //    RESULT: false because 7 is greater than 3

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

q
↑ Back to Top