During the Javascript application process, we will encounter various comparisons. Today, I have compiled three situations for you and learn them together.
1. Comparison of two objects
Javascript comparisons are mixed with some strange features, let's take a look at some simple comparisons.
// Comparison of raw values> var a = 12undefined> var b = 12undefined> a == btrue> a === btrue// Comparison of objects> var c = []undefined> var d = []undefined> c == dfalse> c === dfalse
From the above results, we can see that comparing two original values seems to be a bit different from comparing objects. If you compare the values of two objects, even if their values are the same, the final result is different. When comparing two objects, the references of the two objects should be compared.
If we do this:
> var m = {}undefined> n = m{}> n === mtrueIt 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 == undefinedtrue> undefined == falsefalse> null == falsefalse> null === undefinedfalse
Why can integers be equal to strings? This is because == (equal to, not congruent) does not compare types. Before comparing, it will type the values that need to be compared. Here, the string will be converted to numeric types and then compared . Why is it so sure? Let's do some experiments.
> 1 + 23> 1 + "2"'12'> 2 < "12"true> "2" < "12"false
From the above example, we can see that the value 2 is indeed smaller than the string 12. If the comparison was converted to a string before the value was converted to a string, the result should be "2" > "12".
Don't believe it yet? Let me give you another example.
> "12d" > 12false> "12d" < 12false> "12d" == 12false> "12" < "12d" true
Why is this? If you convert 12 into a string, then 12d should be greater than 12, right? Why do you compare it to false? I guess it is because of the special guy below.
> NaN < 1false> NaN > 1false
No matter what NaN compares with, it will return false. Including itself. Therefore, the best way to determine whether a variable is NaN is x != x If the returned true, it means that x is NaN . Then here it should be when 12d is to be converted into a numeric type because it has special characters and finally becomes NaN. No matter how you compare it with the numeric type, the result is false.
For numeric and string operators, the plus sign operator behaves differently from the comparison operator. The plus operator prefers strings, and if one of the operands is a string, it will be converted to a string. Comparison operators prefer numbers, and string comparisons will only be performed when both numbers are strings.
As for the null and undefined above. . . . . I don't know how to explain their behavior for the time being. I can only remember it for now. Because they are special.
3. Comparison of objects with original values
If two objects that need to be compared are javascript objects and skeletons, a certain degree of type conversion will be performed. Find a way to convert the value of the object into the original value. Generally speaking, there are two methods valueOf, toString . The following is the conversion process of an empty object:
// I wrote it out directly > a = {}{}// 1. valueOf conversion > a.valueOf(){}// 2. If the above operation results in a raw value, then use toString to convert. (And vice versa)> a.toString()'[object Object]'> a == '[object Object]'true The above is actually a built-in object conversion process and a JavaScript mechanism. First it will call valueOf for conversion. If the obtained value is not a primitive value, it will then call toString for conversion. The final value is '[object Object]' a very strange value, but it is the original value. If variable a and this value are equal (not congruent), a true result can be obtained. (Did it collapse?)
However, the authoritative guide gives the following principles, we can refer to it.
Convert raw values JavaScript language core built-in classes first try to convert with ValueO f, and then use toString for conversion. Except for the date class, it only uses toString for conversion. Objects that are not in the core of JavaScript are converted to original values through the way they are defined in their respective implementations.
According to the above explanation. When we compare a={} with the original value, we will call the valueOf function first, and the result is that {} is obviously not a primitive value. Then the conversion will be performed using toString . Finally, that strange result came out. But this strange result '[object Object]' is indeed the original value of {}. (It is the literal of a string).
The above is a comparative summary in Javascript. I hope that the description in this article will be helpful to everyone when learning Javascript.