JavaScript in the application process will encounter a variety of comparisons, today to all of you to organize three kinds of situation, together to learn.
1. Comparison of two objects
JavaScript comparisons are mixed with some of the more bizarre features, so let's take a look at some simple comparisons.
Comparison of original values
> var a =
undefined
> var b =
undefined
> A = = B
true
> A = = B
true
//object Comparisons
> var c = []
undefined
> var d = []
undefined
> c = = d
false
> c = = d
False
From the results above you can see that comparing the two original values to the comparison object rules seems a little different, compared to two object values, even if their values are the same, but the final result is not the same. Comparing two objects, you should compare the references of two objects.
If we do this:
> var m = {}
undefined
> n = m
{}
> n = = M
true
Indicates that two variables all point to the same object, so they are all the same.
2. Different types of comparisons
And here's the special case.
> = = "A"
true
> null = undefined
true
> undefined = =
false
> NULL = = False
false
> NULL = = undefined
false
Why is an integer equal to a string? This is because = = (equal rather than congruent) does not compare types, and it compares the values that need to be compared before, which converts strings to numeric types and then compares them. Why so sure? Let's do the experiment.
> 1 + 2
3
> 1 + "2"
' '
> 2 < "'
true
>" 2 "<" "
false
As you can see from the example above, the value 2 is actually smaller than the string 12. If the comparison is a numeric conversion to a string, then the result should be "2" > "12".
Still don't believe it? Let me give you an example.
> "12d" >
false
> "12d" <
false
> "12d" = =
false
> "<" 12d "
True
What is this for? If you are converting 12 to a string, the 12d should be greater than 12. Why is the comparison all false? I guess it's because of this particular guy below.
> Nan < 1
false
> Nan > 1
false
Nan returns false regardless of what it is compared to. including it himself. So the best way to judge whether a variable is Nan is to x!= x if it returns true it means that X is Nan. So this is supposed to be when 12d is going to convert to a numeric type, because it has a special character and finally becomes Nan, the value type is false regardless of how it is compared.
For numbers and string operators, the plus operator differs from the comparison operator's behavior. The plus operator prefers strings, and if one of the operands is a string, it is converted to a string. Comparison operators prefer numbers, but string comparisons are made only when two 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. Because they are very special.
3. Comparison of objects with original values
If two objects that need to be compared are a JavaScript object and a string, some kind of conversion occurs. Try to convert the value of the object to the original value. In general, there are valueOf, toString
two ways. The following is a conversion process for an empty object:
I wrote it straight out.
> A = {}
{}
//1. valueof conversion
> a.valueof ()
{}
//2. If the above operation does not get a raw value then it is converted with ToString. (and vice versa)
> a.tostring ()
' [Object Object] '
> A = = ' [Object Object] '
true
The above is actually a built-in object of the conversion process, but also the JavaScript mechanism. First it invokes the valueOf
conversion, and if the resulting value is not an original value, then the conversion is called, and toString
the resulting value is '[object Object]'
a very strange value, but it is the original value. If you equate a variable A with this value (not congruent), you can derive a result that is true. (Did it break down?) )
However, the authoritative guide gives the following principles, which we can refer to.
Conversion of raw values JavaScript language core built-in classes first try to use the ValueO
F conversion, and then use toString
the to convert. In addition to the date class, it is only used toString
for conversion. Objects that are not in the JavaScript core are converted to the original values in the way defined in their respective implementations.
According to the above explanation. When we compare our a={} with the original value, we call the valueOf
function first, and the result is {} is obviously not an original value. is used toString
for conversion. Finally came the very strange result. But this very strange result '[object Object]'
is indeed the original value of {}. (It is the literal number of strings).
This is the comparison rollup in JavaScript, and I hope this article will help you learn about JavaScript.