This question already has answers here:
JavaScript - Preventing octal conversion
(3 answers)
Closed 5 years ago.
Any help in understanding how it happens?
console.log(043) // outputs 35
Thanks for any help in advance.
It's using the octal number system, due to the leading zero in the numeric literal, the "0" in number. See http://www.javascripter.net/faq/octalsan.htm
In Javascript, number beginning with 0 are treated as octal number.
Positive octal numbers must begin with 0 (zero) followed by octal
digit(s).
Negative octal numbers must begin with -0 followed by octal digit(s).
35 is the decimal representation of 043.
Related
This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 2 years ago.
console.log(00123 + 34351); //in javascript, outputs 34434
print(00123 + 34351); //in python, outputs 34434
in javascript, outputs 34434
in python, outputs 34434
However removing the leading zeroes i.e (123 + 34351) gives the correct answer, which is 34474
0 is a common prefix for octal, for which the decimal number is 83. Doing
console.log(00123+34351)
is equivalent to
console.log(83+34351)
edit: note that in python 3+ the prefix is 0o
Not sure about Java, but in JavaScript and in Python 2, numbers starting with 0 are octal numbers, i.e. 00123 is actually 83 in decimal.
JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Numeric_literals
No, the correct answer is, indeed 34434 (base 10). A number with a leading 0 in many languages is octal, so your addition operation is 123 (base 8) + 34351 (base 10). Convert the octal integer to decimal, and you'll see the rationale.
This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 3 years ago.
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))
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)
The function has nothing to do with it.
The JavaScript compiler converts your number literal into a Number when it compiles the source code.
Since the number starts with a 0, it is treated as octal instead of decimal.
This question already has answers here:
JavaScript - Preventing octal conversion
(3 answers)
Javascript, why treated as octal
(6 answers)
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 3 years ago.
In reviewing parseInt(string, radix) in:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
all 13 examples make perfect sense except for this one.
According to one example, parseInt(015, 10) will return 13. This makes sense assuming that numericals that begin with 0 are treated as an octal, regardless of the 10 that appears in the radix position.
So if the octal is specified as it is in the question header:
parseInt(021, 8)
Then why wouldn't this be 17 (vs. 15 per the Mozilla documentation and in my tests in jsfiddle?
Any insight would be appreciated.
This is because 0n is octal notation in javascript, just like Oxn is hex notation:
console.log(021 === 0x11 && 021 === 17);
So what you wrote got evaluated as parseInt(17, 8);
Then this 17 number gets coerced to the string "17" and the result is 15.
console.log(parseInt(17, 8))
Note that all this would not have happened in strict mode, where 0n notation has been deprecated:
(function(){
"use strict";
// Syntax Error
console.log(parseInt(015, 8))
})();
The first argument of parseInt should be a string. The same MDN link says:
The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation).
You can see that this works as expected:
console.log(parseInt("021", 8))
The problem in your tests is that you're using a number, not a string. When you use 021 as a number, as you already knows (when you said "numericals that begin with 0 are treated as an octal"), it gets converted to "17":
console.log(021.toString())
And that gives you the result you're seeing:
console.log(parseInt("17", 8))
When you type 021 then this is valid octal number and because prefix 0 JS convert it to decimal 17. But if you type not valid octal number e.g. 019 then JS will NOT convert it but treat as decimal
console.log(021) // octal
console.log(019) // not octal
if( 021 < 019 ) console.log('Paradox');
This question already has answers here:
Prefix zero changes output in number addition [duplicate]
(2 answers)
Closed 9 years ago.
I have encountered a very weird issue with my JavaScript program. I have fount that JavaScript for some reason changes 040000 into 16384! [Example] Does anyone know why JavaScript is doing this?
It's because in js, number literals prepended with 0 are considered octal (base 8)
For example
010 == 8
In your example 040000 is really 4*8*8*8*8 = 16384 because in octal each 0 in the right multiplies the value by 8.
EDIT: Bonus:
If the leading 0 is in a string representation, (for example, if it was introduced by the user), and you want to avoid converting to octal, specify the base (aka radix) with value 10 in the parseInt method call, like this
var number = parseInt("040000", 10); //number will be 40000 ;)
In recent browsers, the radix is 10 by default, but not in old browsers, so if you want maximum compatibility also, always specify the radix parameter (usually 10).
Cheers
Because javascript thinks its in OCTAL format
Explanation:-
Javascript (like most programming languages) allows us to work directly with both octal and hexadecimal numbers, all we need is a way to tell which number base we are using when we specify a number. To identify octal and hexadecimal numbers we add something to the front of numbers using those bases to indicate which base we are using. A leading 0 on the front of a number indicates that the number following is octal while a leading 0x indicates a hexadecimal number. The decimal number 18 can therefore also be represented as 022 (in octal) and 0x12 (in hexadecimal). We don't put a special symbol on the front of decimal numbers so any number that doesn't start with 0 or 0x is assumed to be decimal.
So its same in your case
040000(base8)=16384(base10)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Leading zero in javascript
Given you have the following JavaScript snippet:
<script>
var x = 013;
console.log(x);
</script>
Why is it that Firebug prints 11?
JavaScript supports the same convention for numeric constants as C and Java (et al), so the leading zero makes that an octal constant. ("13" in base 8 is 8 + 3, or 11.)
Its octal value. So it 1*8^1 + 3*8^0=11
Because you've specified an octal number, but it displays its decimal representation.
It's interpreting it as octal. Any number that begins in zero is interpreted as an octal (base 8) literal, and octal 13 = 8*1+3 = 11 decimal.
Also, good title.
013 is an octal constant - it's interpreted in base 8. 1·8 + 3·1 = 8 + 3 = 11
It is being interpreted as base 8. And 013 in base 8 is 11 decimal.
In javascript, constant numbers that begin with 0dd or -0dd and are not 0xdd or -0xdd are interpreted as octal (base 8).
You can see it described in the ECMAScript specification on page 231.