Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a JavaScript function automatically convert a binary number when I pass it as an argument? [duplicate]

Tags:

javascript

I guess I can format it back. I'm just interested in why it's happening.

function test(d){
 console.log(d) // 151028224
}
console.log(test(00001100101000))
like image 243
Kevin Avatar asked Mar 14 '26 02:03

Kevin


1 Answers

By default, any number literally written with a zero at the beginning is considered as octal (base 8) number representation, and then, when you show back any number with console.log, it is written as its base 10 representation.

console.log(05)
console.log(06)
console.log(07)
console.log(010)
console.log(011)

It's recommended to avoid this in code, because it can lead to confusions :

if the number contains the digits 8 or 9, it cannot be a base-8 number, and thus treated as base 10 !

console.log(05)
console.log(06)
console.log(07)
console.log(08) // Yiiik !
console.log(09) // Yiiik !
console.log(010)
console.log(011)
like image 102
Pac0 Avatar answered Mar 15 '26 14:03

Pac0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!