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

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)

Related

Javscripts prints 1024 when I console.log(02000) [duplicate]

This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Number with leading zero in JavaScript
(3 answers)
Closed 10 months ago.
I am working with an array of numbers
let nums = [02000, 02000, 02200, 02020,02002]
The problem is when I console.log(nums) it prints out [1024,1024,1152,1040,1026].
Why is it changing my numbers?
In JavaScript, Numeric Literals have their own lexical grammar.
When numbers start with a leading 0, it may be interpreted as an octal.
Note that decimal literals can start with a zero (0) followed by another decimal digit, but if all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number.

console.log weird behaviour with numbers [duplicate]

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.

Javascript Eval error with numbers having leading zeroes [duplicate]

This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 5 years ago.
If I use the code:
string = '010';
write = eval(string);
document.write(write)
I get 8 written on the page. Why?
This happens even if 010 isn't a string.
Because 010 is parsed as octal. Javascript treats a leading zero as indicating that the value is in base 8.
Similarly, 0x10 would give you 16, being parsed in hex.
If you want to parse a string using a specified base, use parseInt:
parseInt('010', 8); // returns 8.
parseInt('010',10); // returns 10.
parseInt('010',16); // returns 16.
Prefixing a number with an 0 means it's octal, i.e. base 8. Similar to prefixing with 0x for hexadecimal numbers (base 16).
Use the second argument of parseInt to force a base:
> parseInt('010')
8
> parseInt('010', 10)
10
If you'd like to output the string 010 to the document, you can wrap the value in quotation marks:
var octal = eval('"010"');
typeof octal; // "string"
If you want to parse an integer or understand octals, read the other answers.

Leading zeros leading to wrong sorting answer using inbuilt sort method of javascript [duplicate]

This question already has answers here:
Javascript - Leading zero to a number converting the number to some different number. not getting why this happening?
(2 answers)
Closed 5 years ago.
I was sorting a string/array of integers in lexicographical order. A case came when i had to sort a string containing "022" using array.sort. I don't know why it is equating that equal to "18" when being printed.
var l = [022,12];
l.sort();
(2) [12, 18] => output
What is the reason behind this and how to correct it?
I recommend to "use strict"; so that 022 will produce a syntax error instead of octal number:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal
This isn't specific to the sort. If you just type 022 into a console, you'll get back 18. This is because 022 is being interpreted as an OctalIntegerLiteral, instead of as a DecimalLiteral. This is not always the case however. Taking a look at the documentation:
Note that decimal literals can start with a zero (0) followed by another decimal digit, but If all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number. This won't throw in JavaScript, see bug 957513. See also the page about parseInt().
EDIT: To remove the leading 0s and interpret the 022 as a decimal integer, you can use parseInt and specify the base:
parseInt("022", 10);
> 22

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

Categories

Resources