JavaScript adding a string to a number

后端 未结 8 767
轮回少年
轮回少年 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: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
    

提交回复
热议问题