DEV Community

ericksong91
ericksong91

Posted on • Updated on

Reviewing Javascript Comparisons

Hi y'all,

This is where I'm going to be taking some notes on specific Comparison operators in Javascript. Typing this out will help me remember the specifics and may also be useful to you as a reference sheet! I will also be attempting to transliterate what the code is saying to make it easier to read.

Equality and Inequality Operators

The Strict Equality Operator (===)
Equal without any type conversions

60 === 60;
// 60 (number) is strictly equal to 60 (number)
> true


60 === "60";
// 60 (number) is strictly equal to 60 (string)
> false

Enter fullscreen mode Exit fullscreen mode

The Strict Inequality Operator (!==)
Unequal without any type conversions

60 !== 60;
// 60 (number) is strictly not equal to 60 (number) 
> false


60 !== "60";
// 60 (number) is strictly not equal to 60 (string) 
> true
Enter fullscreen mode Exit fullscreen mode

The Loose Equality Operator (==)
Equal with any type conversions (with extra examples)

*Extra examples aren't necessarily accurate but they're true in the confines of the coding language

60 == "60";
// 60 (number) is loosely equal to 60 (string) 
> true

true == 1;
// true is loosely equal to 1 (number)
> true

"0" == false;
// 0 (string) is loosely equal to false
> true

null == undefined;
// null is loosely equal to undefined
> true

" " == 0;
// Empty Quotations is loosely equal to 0 (number)
> true
Enter fullscreen mode Exit fullscreen mode

The Loose Inequality Operator (!=)
Inequal with any type conversions

60 != "60";
// 60 (number) is not equal to 60 (string)
> false

61 != 60;
// 61 (number) is not equal to 60 (number)
> true

Enter fullscreen mode Exit fullscreen mode

Relational Operators

  • greater than (>)
  • greater than or equals (>=)
  • less than (<)
  • less than or equals (<=)

*Important to note that Javascript will attempt to convert strings into numbers here

*For the last example, note that if both sides of the inequality are strings, it will only compare them lexicographically (left to right, by each character). It will NOT convert the whole string into a number.

70 > "60";
// 70 (number) is greater than 60 (string)
> true

70 >= 70;
// 70 (number) is greater than or equal to 70 (number)
> true

70 < "80";
// 70 (number) is less than 80 (string)
> true

70 <= "71";
// 70 (number) is less than or equal to 71 (string)
> true

"70" > "8";
// 70 (string) is greater than 8 (string)
> false (see note above)

Enter fullscreen mode Exit fullscreen mode

Sources

MDN:

Top comments (0)