This question already has answers here:
Javascript 0 in beginning of number
(3 answers)
Closed 5 years ago.
Javascript leading 0 on integers getting wrong value on console.log.
Why I am getting like this ?
Code:
console.log(456);
console.log(0456);
Output:
456
302
Because JS "translates" 0456 as an octal value, since it has a trailing zero and all its digits < 8.
Related
This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 2 years ago.
Have some doubts about the javascript. when I try to console
a = 021
console.log(a) // 17
why it is changing and how to store the exact value in the variable
Because with leading zero it's a octal-number so 21 is 2*8+1 = 16.
But when you print it out it's in decimal.
This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 5 years ago.
We're having a discussion in the office about how the hell this math works in JavaScript.
There was an instance where we were multiplying by 010 instead of 10 and this gave the incorrect returned value.
For example...
25.25 * 010 = 202
25.25 * 10 = 252.5 as expected
whats even weirder is if you do parseFloat(010) it gives you 8!
For 010 is decimal 8, so it get 202.
console.log(25.25 * 010);
Look at this answer for Java: Why "010" equals 8?. JavaScript has to do the same.
The answer is, that an octal number starts with a leading zero 0.
This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 7 years ago.
I want to declare
var a = 0213;
document.write(a);
but it not work .when console.log(a); variable
different.
Numbers with a leading 0 are interpreted as octal (base 8).
Octal 0213 == decimal 139.
If you need the value 213 (decimal), leave off the leading 0.
This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
I have a function where I passes the value dynamically
0011
In javascript am just passing this value and it returns me 9
JS
function searchError(s){
alert(s);
}
Need help to understand why ?
I fixed it by quoting the value like
0011
JS Fiddle
0011 is an octal number since it has a leftmost 0 so its equal to 0 x 82 + 1 x 81 + 1 x 80 = 9. Originally the value was interpreted as a numeric. Enclosing it in quotes caused it to be treated as a String literal.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to parseInt a string with leading 0
Workarounds for JavaScript parseInt octal bug
How can I parse the "09" into number?
alert(parseInt("09"));
This returns me 0 ..Why is that and how do I fix this?
Specify the base as well:
alert(parseInt("09", 10)); // outputs 9