前缀零会在数量增加时更改输出

Man*_*ava 2 javascript

我试图在Javascript中添加两个数字:

 var output;
 output = parseInt(a)+parseInt(b);
 alert(output);
Run Code Online (Sandbox Code Playgroud)

它给出了错误的output价值,例如if a = 015b = 05.为什么会这样?上述例子的预期结果应为20.

phi*_*hag 7

如果您在数字前加上0,则在基数8中指定它们,015因此为13,总和为18.

使用第二个parseInt参数强制基数:

var a = '015', b = '05';
var output;
output = parseInt(a, 10) + parseInt(b, 10);
alert(output); // alerts 20
Run Code Online (Sandbox Code Playgroud)