Javascript返回奇怪的价值
问题描述:
我正在测试一个函数,看看当它的参数是 null
时会发生什么,并决定用它添加一个else语句。令我惊讶的是,它没有记录我通过的参数,它完全记录了其他内容。也许有人可以对此有所了解,这里是代码:
I was testing a function out to see what happens when it's parameters are null
and decided to put an else statement with it. To my surprise, it did not log the parameters that I have passed, it's logging something else entirely. Maybe someone can shed some light on this, here's the code:
function testing(o) {
if (!o) {
return "Sorry, looks like you need to pass an argument.."
} else {
return o;
}
}
console.log(testing(02034));
//logs 1052
这里发生了什么?
答
在Javascript中,与其他语言一样,以0开头的数字表示它的基数为8(八进制)。
In Javascript, like other languages, starting a number with 0 would indicate it's base 8 (Octal).
因此, 02034
基数为8 = 1052,基数为10(十进制)。
Thus, 02034
in base 8 = 1052 in base 10 (decimal).