String numbers together

// If a string or strings are two, using the "+" will be spliced instead of being added; 
    // if a "-" will be forced to do the subtraction after conversion 
    var Result = 12 is + + 2 "12" - 2 * 2;   // 1408 subtractor coerced 
    document.write (Result);
     var RESULT1 + 2 + 12 = "12";     // 1412 adder splicing 
    document.write (result1);

Therefore, before adding the number string, must first be transformed, it is not directly added to
1. The easiest way is to add one of the preceding "+"

let str1 = '1234'
let str2 = '3456'
let res = (+str1) + (+str2)

2. A second simple method, can be reduced 0

let str1 = '1234'
let str2 = '3456'
let res = (str1 - 0) + (str2 - 0)

 

Guess you like

Origin www.cnblogs.com/qihang0/p/11494827.html