JS Implicit conversion Personal Summary

Source: Internet
Author: User

Implicit conversions in JS can usually be converted into corresponding data types through functions such as parseint (), number (), String (), and Boolean (), but usually for simplicity, we use a special notation, such as:

var a = 5;console.log (A + ");    Output ' 5 ', turn number type to string type

  

Here is the string converted to number type Console.log (+ ' + ');    Output 56, equivalent to number (a) console.log (-' n ');     Output -56console.log (+ ");      0console.log (+ ' 34A '); Output nan Console.log (-' 34A '); Output NaNconsole.log (parseint (' 34A ', 10)); Output 34; parseint The second parameter is best passed in 10, if not, in ES3, the default is converted to 8 binary console.log (+[]);    

  

  

Turns a string or number into a Boolean value of Console.log (!5);      False, this process will first turn 5 to true, then take the non-console.log (!! 5);     True, equivalent to Boolean (5) console.log (!! ') 2 ');   Trueconsole.log (!! ");    Falseconsole.log (!! 0);     Falseconsole.log (!! [] );    Trueconsole.log (!! {} );    Trueconsole.log (!! NULL);      Falseconsole.log (!! undefined); Falseconsole.log (!! /abc/g  );   True

Summarize:

    1. Any data type and string are added together, resulting in a string.
    2. Only an empty array (if there is an element in the array, and this element can only be converted to 0), an empty string, a string full of spaces, false will be converted to 0, the other look at the situation
    3. Only null, undefined, empty string, 0, Nan will be converted to false, all others are true
    4. If you can make sure that the string is made up of pure numbers, you can save time and effort by converting it to number type in front with "+". (Many of these are written in jquery)
    5. The judgment statement still uses the = = = to carry on the strict judgment, avoids the unnecessary trouble

So, [] = =! [] Get the true, because the right one! [] will be converted first to false, and then because it is = = and the data type is different on both sides, so the two sides will be converted to number for comparison, the left [] to number is 0, the right false also turns to 0, so equal returns True

One thing to be aware of: null = = undefined the comparison gets true

The above is purely personal summary, if there are errors, please correct me.

JS Implicit conversion Personal Summary

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.