JavaScript adding a string to a number

后端 未结 8 612
轮回少年
轮回少年 2020-11-29 04:12

I was reading the re-introduction to JavaScript on MDN and in the section Numbers it said that you can convert a string to a number simply by adding a plus operator

相关标签:
8条回答
  • 2020-11-29 04:21

    When you look at Step 7 and "Note 2" at The Addition operator ( + ) (§11.6.1) in the ES5 specs,

    it says

    If Type(lprim) is String` or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)


    NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation.

    Meaning if either 7 (3+4) or "5" (|| not &&) is typeof "string" toString() is applied to both operands.

    So the addition is actually applied to

    "7" and "5" -> "7" + "5" //"75"

    0 讨论(0)
  • 2020-11-29 04:22

    The answer can be found in Ecma262.pdf section 11.6.1:

    If Type(lprim) is String or Type(rprim) is String, then a. Return the String that is the result of concatenating ToString( lprim) followed by ToString(rprim).

    So that will resolve all operations according to precedence, so that as soon the string is found any number, the number is converted to string.

    4 + 3 + "5"
    "75"
    4 + 3 + "5" + 3
    "753"
    

    To read the whole standard, go here.

    0 讨论(0)
  • 2020-11-29 04:24

    What you are talking about is a unary plus. It is different than the plus that is used with string concatenation or addition.

    If you want to use a unary plus to convert and have it added to the previous value, you need to double up on it.

    > 3 + 4 + "5"
    "75"
    > 3 + 4 + +"5"
    12
    

    Edit:

    You need to learn about order of operations:

    + and - have the same precedence and are associated to the left:

     > 4 - 3 + 5
     (4 - 3) + 5
     1 + 5
     6
    

    + associating to the left again:

    > 3 + 4 + "5"
    (3 + 4) + "5"
    7 + "5"
    75
    

    unary operators normally have stronger precedence than binary operators:

    > 3 + 4 + +"5"
    (3 + 4) + (+"5")
    7 + (+"5")
    7 + 5
    12
    
    0 讨论(0)
  • 2020-11-29 04:30

    A smple + operator in javascript is used for concatenation and not to add.

    A bracket and a + operator before the string format integer variable will do the job every time.

    This always works fine.

    1 + (+"2") = 3
    

    Because placing + before string converts the string variable into number.

    fiddle here: http://jsfiddle.net/xuVur/2/

    0 讨论(0)
  • 2020-11-29 04:35

    When you add a number and a string in Javascript the result is always a string.

    Check the experiment here - http://jsfiddle.net/xuVur/1/

    var a = "3" + 4 + 5;
    var b = 3 + 4 + "5";
    
    $("#result1").html(a); // prints 345
    $("#result2").html(b); //prints 75
    
    0 讨论(0)
  • 2020-11-29 04:38

    It is confusing, but binary + seems to be a rare case of a number being coerced to a string. In most cases it would be the other way round (so it's very easy to think that you don't need to be too fussy about the type)

    4+"3" = "43"

    4-"3" = 1

    0 讨论(0)
提交回复
热议问题