Multiple numeric and numeric string blending rules

Source: Internet
Author: User

Before we say the rules, let's take a look at the results of the following code:

1Console.log (1 + "2" + "2");//1222 3Console.log (1 + + "2" + "2");// +4 5Console.log (1 +-"1" + "2");// Geneva6 7Console.log (+ "1" + "1" + "2");// the8 9Console.log ("A"-"B" + "2");//NaN2Ten  OneConsole.log ("A"-"B" + 2);//NaN

Rules:

1. Multiple numeric and numeric string blending operations are related to the position of the operand.

Such as:

Console.log (2 + 1 + ' 3 '); //' 33 '

Console.log (' 3 ' + 2 + 1); ' 321 '

2. A numeric string is converted to a number when it is preceded by a positive sign (+/-) in the number

Such as:

Console.log (typeof ' 3 ');   String

Console.log (typeof + ' 3 '); Number

3. You can add a number to a string before the number

Such as:

Console.log (typeof 3);   Number

Console.log (typeof (' +3)); String

4. If the result of the operation cannot be converted to a number, it will return NaN

Such as:

Console.log (' a ' * ' SD ');   NaN

Console.log (' A '-' B '); NaN

Multiple numeric and numeric string blending rules

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.