Comparative summary of Javascript

  • 2021-07-04 18:12:10
  • OfStack

In the process of Javascript application, we will encounter various comparisons. Today, we have sorted out three situations for everyone. Let's learn.

1. Comparison of two objects

The comparison of Javascript is mixed with one of the weirder features. Let's look at one of the simpler comparisons.


//  Comparison of original values 
> var a = 12
undefined
> var b = 12
undefined
> a == b
true
> a === b
true
//  Comparison of objects 
> var c = []
undefined
> var d = []
undefined
> c == d
false
> c === d
false

It can be seen from the above results that the rules for comparing two original values and comparing objects seem to be a little different. If comparing two object values, even if their values are the same, the final results are different. When comparing two objects, what should be compared is the references of the two objects.

If we do this:


> var m = {}
undefined
> n = m
{}
> n === m
true

Means that both variables point to the same object, so they are the same.

2. Comparison of different types

There is also the following special situation


> 12 == "12"
true

> null == undefined
true
> undefined == false
false
> null == false
false
> null === undefined
false

Why can an integer be equal to a string? This is because = = (equal to but not congruent) does not compare types, it converts the values to be compared before comparing, in this case, it converts strings to numeric types and then compares them. Why are you so sure? Let's do an experiment.


> 1 + 2
3
> 1 + "2"
'12'
> 2 < "12"
true
> "2" < "12"
false

As can be seen from the above example, the value 2 is indeed smaller than the string 12. If the comparison was preceded by a numerical conversion to a string, the result should be "2" > "12" is right.
Still don't believe it? Let me give you another example.


> "12d" > 12
false
> "12d" < 12
false
> "12d" == 12
false
> "12" < "12d"
true

Why is this? If 12 is converted into a string, then 12d should be greater than 12. Why is it all false? I guess it's because of this special guy below.


> NaN < 1
false
> NaN > 1
false

NaN returns false no matter what it is compared to. Including itself. So the best way to judge whether a variable is NaN is x! = x If true is returned, x is NaN. Then it should be that when 12d is converted to a numeric type, it finally becomes NaN because of its special characters. No matter how compared with the numeric type, the result is false.

For the number and string operators, the plus operator behaves differently from the comparison operator. The plus operator prefers strings, which are converted to strings if one of the operands is a string. Compare operators, on the other hand, prefer numbers, and string comparisons are performed only if both numbers are strings.
As for the above null and undefined. . . . . I don't know how to explain their behavior for the time being. I can only remember it for the moment. Because they are special.

3. Compare the object to the original value

If two objects to be compared, one is an javascript object and one is a string, some type conversion is performed. Find a way to convert the value of the object to the original value. 1 generally speaking, there is valueOf, toString Two ways. The following is the conversion process of an empty object:


//  I wrote it directly in plain text 
> a = {}
{}
// 1. valueOf  Conversion 
> a.valueOf()
{}

// 2.  If the above operation is not 1 The original values are then used toString  Conversion. ( And vice versa )
> a.toString()
'[object Object]'
> a == '[object Object]'
true

The above is actually a built-in object conversion process, which is also the mechanism of javascript. First, it calls the valueOf If the resulting value is not 1 original value, it will then call the toString Convert, and the final value is '[object Object]' A very strange value, but it is the original value. If the variable a is equal to this value (not congruent), the result of true can be obtained. (Did it break down? )

However, the authoritative guide gives the following principles, which we can refer to under 1.

The JavaScript language core built-in class first tries to use the ValueO f conversion, and then use the toString Make a conversion. In addition to the date class, it only uses the toString Make a conversion. Objects that are not in the JavaScript core are converted to their original values in the manner defined in their respective implementations.
According to the above explanation. When comparing our a= {} with the original value, we will call the valueOf Function, and its result is that {} is obviously not an original value. Will use the toString Make a conversion. Finally, I got that strange result. But this is a strange result '[object Object]' Is indeed the original value of {}. Which is the literal value of a string.

The above is the comparison summary in Javascript. I hope this article will be helpful for everyone to learn javascript.


Related articles: