The difference between js = = = = = =

Source: Internet
Author: User

1, for String,number and other basic types, = = and = = = There is a difference

1) Comparison of different types, = = Comparison of "converted to the same type of value" to see if "value" is equal, = = = If the type is different, the result is not equal to 2) the same type comparison, direct "value" comparison, the results of the same 2, for Array,object and other advanced types, = = and = = = There is no difference betweenMake a "pointer address" comparison 3, the basic type and the advanced type, = = and = = = There is a difference1) for = =, the advanced conversion to the underlying type, the "value" Comparison 2) because the type is different, = = = The result is False

Sometimes you see that you use three equals sign (= = =) When judging whether two objects are equal, what is the difference between it and two equals sign (= =)? In simple terms, when using "= =", if the two types are different, the JS engine converts them to the same type and then compares them, while "= = =" Does not convert the type, so when the two sides are not of the same type, it is certainly not equal. For example:

var a = 0, B = ' 0 '; alert ((A = = B) + '--' + (a = = b))

The result you see is "True–false".

= = = Judgment Rule

    1. If the types are different, [unequal]
    2. If two are numeric and are the same value, then [equal]; Exception) is, if at least one of them is Nan, then [unequal]. (Determine if a value is Nan and can only be judged by isNaN ())
    3. If all two are strings, the characters are the same for each position, then [equal]; otherwise [unequal].
    4. If all two values are true, or both are false, then [equal].
    5. If all two values refer to the same object or function, then [equal]; otherwise [unequal].
    6. If two values are null, or both are undefined, then [equal].

= = Judgment rule:

    1. If the two value types are the same, the = = = comparison.
    2. If two value types are different, they may be equal. Type conversions are then compared according to the following rules:
    • If one is null and one is undefined, then [equal].
    • If one is a string, one is a numeric value, the string is converted to a numeric value and then compared.
    • If either value is true, convert it to 1 again, and if either value is false, convert it to 0 and then compare.
    • If one is an object and the other is a numeric or string, the object is converted to the value of the underlying type and then compared. The object is converted to the underlying type, using its ToString or ValueOf method. JS Core built-in class, will try to valueof before ToString, the exception is that date,date is using the ToString conversion. Non-JS core of the object, so that (more trouble, I do not understand)
    • Any other combinations, are [unequal].

It is particularly important to note the conversion of true, FALSE, for example:

Alert (true = = 1);  Turealert (true = = 2); False, True converts to number, which is 1, of course 1 is not equal to 2//can use!! To convert a data type to a Boolean alert (true = =!!). 2)//true,!2 = = = False! (!=2) =!false = True

In addition, in JS, if a variable is used in a logical operation, then the value of the variable is false if there is no initial value or if its value is 0, 0, NULL, "", false, undefined, or Nan. Otherwise, its value is true.

The difference between js = = = = = =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.