console.log weird behaviour with numbers [duplicate] - javascript

This question already has answers here:
Console.log output in javascript
(6 answers)
Closed 3 years ago.
console.log(01) results in 1
But
console.log(011) results in 9
Can someone explain how console.log works with such numbers?

It's not about console.log, a number that starts with 0 is octal notation

console.log(+"011") // if you use like this it will work
011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.

Related

console.log shows different number (11321144241322243122) [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 1 year ago.
When i try to log this number 11321144241322243122 into the console it always gets converted to this number 11321144241322244000. This happens in node.js and in the browser console.
11321144241322243122 is bigger than Number.MAX_SAFE_INTEGER.
Instead, convert it to a BigInt:
console.log(BigInt("11321144241322243122").toString())
console.log(11321144241322243122)

Why is js parseInt not working for this particular integer? [duplicate]

This question already has answers here:
How is the parseInt in JavaScript defined to handle large "numbers" - is there an ECMA leak? I got a wow here
(3 answers)
Why is parseInt() not converting my String of numbers correctly?
(1 answer)
Closed 4 years ago.
Here is my string: "6145390195186705543"
I have tried parseInt with different radix values. I have tried the Number() method. And I have tried multiplying it by one. They all give back 6145390195186705000
Why is this??

Number literal with leading zero does not result in expected value [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 7 years ago.
I have this code:
console.log(066); // 54
Why does it log 54, not 66?
In JavaScript, numeric literals that begin with a 0 are treated as octal.
From the MDN docs:
Octal number syntax uses a leading zero. If the digits after the 0 are outside the range 0 through 7, the number will be interpreted as a decimal number.
Because add a prefix 0 will make the number to be considered of base 8(octal), as way 0x will make the following number to be of base 16(hexa)

Converting from number to string gives strange number in Javascript [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?
Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal

How come parseInt("08") = 0, parseInt("07") = 7 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function parseInt() doesn't parse numbers with leading 0 correctly
Strange issues when parsing in JS occur.
parseInt("08")
//The result is: 0
parseInt("07")
//The result is: 7
Why is this happening?
Because of the 0 prefix. It tells Javascript the number is Octal, in base-8. 8 isn't a legal octal digit.
Use parseInt("8") instead, or as #Gumbo so correctly pointed out - parseInt("08", 10)

Categories

Resources