JavaScript – Do you really understand comparisons in JavaScript?

Tram Ho

Question


Javascript is currently one of the hottest languages ​​today because of its popularity. Almost any programmer, if he wants to become a Web developer, has to learn through. You can take 1 to 2 weeks to learn and write Javascript. However, to really understand javascript you need a long exposure and work with it. Let’s look at some of the following examples:

Continue:

You can use console.log to see the results. Began to feel confusing, right?

The comparison is equal


Comparison === (! ==)

Many of us still think === comparison is comparing the same type as well as data. This is basically true even for null and undefined . But there are still not true cases. As:

In fact, the === comparison follows the following rules (as far as I learn, it may not be complete):

  • If the two sides of the comparison are of a different type, the result is false
  • When both sides are Number , if either side is NaN , the result is false . Otherwise compare values
  • When both sides are String, then compare the content, if the same content is true , the rest is false
  • When both sides are Boolean , the same true or false is true and the rest is false
  • When both sides are reference Object type, if both sides refer to 1 Object
  • When both sides are special types such as null , undefined , if the same null or the same undefined is true , the rest is false

Comparison == (! =)

The == comparison also follows the following rule:

  1. If the two sides have the same data type, then refer to the comparison ===
  2. Otherwise, if the two sides are different from the data type:
  • If one of the two sides is Number , the other side is of type String , String will be converted to Number for comparison
  • If 1 of the 2 sides is Number , the other side is Boolean , Boolean will be converted to Number for comparison.
  • If one of the two sides is a String and the other is Boolean , both sides will be converted to Number for comparison.
  • If one of the two sides is a String , the other is the reference Object type, the reference Object will be passed to the String for comparison.
  • If one of the two sides is Number , the other side is the reference Object type, the reference Object will be converted to Number for comparison.
  • If both sides are null or undefined including one side is null , the other side is undefined then it is true.

The comparison is less


Less comparisons, including: > , < , >= , <= , are all subject to the following rules:

  • Case 1: Both sides are the same type
    • Both sides are of the same type Number , compare by value
    • Both sides are of the same type String , compared in order of UniCode
  • Case 2: Both sides are different, so will follow the following rules:
    • If one of the sides is of type Number and the other side can be converted to Number , convert it to Number and compare the value. Specifically, if 1 of 2 is NaN , the result is false
    • If one of the two sides is of type String and the other side can be converted to String , convert to String and compare by Unicode.
    • If one of the two sides is of type Number , the other side is of type String , the type of String will be converted to Number and compare the numeric value.
    • In particular, if one of the two sides cannot be converted to Number or String or when it becomes a NaN value, the result will be false.

summary


Above is just a small part for me to refer or find out. Maybe it will be incomplete or many shortcomings, so I hope to receive your contributions to make the article more complete. Hopefully the article will be useful to programmers who are on their way to learn about Javascript. Thank you for watching!

Share the news now

Source : Viblo