Javascript Arithmetic Operation on Numeric Strings

Feb 24, 2014

In an statement involving the ‘+’ operator on a numeric string and an int, JavaScript converts the int into a string and perform string concatenation. For all other arithmetic operators, the numeric string is converted to a number. This is elaborated with the example bellow.

console.log ( "12" + 6 ) ; // 126
console.log ( "12" - 6 ) ; // 6
console.log ( "12" * 6 ) ; // 72
console.log ( "12" / 6 ) ; // 2

console.log ( 6 + "12" ) ; // "612"
console.log ( 6 - "12" ) ; // -6
console.log ( 6 * "12" ) ; // 72
console.log ( 6 / "12" ) ; // 0.5