Conversion rules for JS data types

Source: Internet
Author: User
Tags array to string string to number

Rules for data type conversions
  • 1 if there is only one value, judging whether this value is true or FALSE, follow only 0,nan, ' null,undefined these five are false and the rest are true
  • 2 If two value comparisons are equal, follow these rules:
      • ![] ->false
      • []==[]->false
      • //val==val Two values may not be of the same data type, if two equals sign comparison, The default data type conversion
      • rule:
        • 1 Object = = object is never equal
        • 2 Object = = String
          • First convert the object to a string (method called ToString), Then compare
            • [] empty array to string is '
            • {} Convert to String "[Object,objec]"
        • The
        • 3 Object = = Boolean type
          • object first converts the string (tostring method), then converts the string to a digit (number, "" The empty string is 0), the Boolean type is converted to a number (true is 1,false is 0), and finally two numbers Word comparison []==false->true
        • 4 Object = = Number
          • First convert the string to a string (toString) and then convert the string to Number, and then compare
        • 5 Number = = Boolean
          • convert Boolean to number
        • 6 Number = = String The
          • string is converted to a number
        • 7 String = = Boolean
          • all converted to numbers
        • 8 null==undefined result is true;
        • 9 null or undefined and any other data type comparisons are not equal
  • 3 except = = is compared, = = = Comparison (absolute comparison)
    • val = = = Val2 Compares data types if the data types are different and certainly not equal

===============================================================================================================================

Two values are compared, and if the data type is different, the default data type conversion

Object = = object: Compare the memory addresses of the respective representatives, the same address, and the different addresses are unequal

Object = = Number: First the object is ToString, then the result is the number, and the final comparison with the numbers

Object = = String: The object is first ToString and then compared

Object = = Boolean: All converted to digital

-String = = Number: string converted to Digital

-String = = Boolean: All converted to digital

Boolean = = Number: Boolean converted to Digital

->null = = undefined are equal, they are equal to themselves, besides and any other value do not want to wait

->nan = = NaN is not equal

Converts a value to a Boolean type: The!、!!、 boolean, if (num) condition means that you first convert num to Boolean to verify true or False

Only ', 0, NaN, null, undefined for false, the rest is true

! [] = = false: two = = The priority of the judgment is the lowest, it must be the right and left side of the expression, the results of the implementation of the comparison

! [] Convert to Boolean and then take back->false =>true

Conversion rules for JS data types

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.