在javascript中输出Console.log [英] Console.log output in javascript
问题描述
为什么 console.log(00);
和 console.log(01);
print 0& 1在浏览器控制台而不是00& 01?
console.log(00); //打印0;
console.log(01); //打印1;
console.log(011); //打印9;
console.log(0111); //打印73;
绝不写数字领先零(如07)。
有些JavaScript版本如果用前导零编写,则将数字解释为八进制。
这是因为JavaScript处理前导0作为八进制数,这就是为什么你得到八进制数(基数为8)。
你可以使用
Why do console.log(00);
and console.log(01);
print 0 & 1 in the browser console and not 00 & 01?
console.log(00); // prints 0;
console.log(01); // prints 1;
console.log(011); // prints 9;
console.log(0111); // prints 73;
Never write a number with a leading zero (like 07). Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
That is because JavaScript treats leading 0 as octal number and that is why you are getting the octal number(base 8).
You could use parseInt with radix to eliminate these kind of issues.
And the reason why console.log treats the input as octal is, by default console.log calls valueOf
method of input. If it doesn't returns anything it will call toString
method.
And the valueOf
method returns values like below:
00.valueOf() // 0
01.valueOf() // 1
011.valueOf() // 9
0111.valueOf() // 73
Reference:- http://javascript.info/tutorial/object-conversion
For your reference I've added the number system table
Number System table
这篇关于在javascript中输出Console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!