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.
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:
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
EXTREMELY confused here.
parseInt("09") = 0
but
parseInt("9") = 9
Why is the prefixed zero not just stripped out?
alert(parseInt("01")); = 1
.. rage quit
Because that is treated as octal format, by default. If you want to get 9, you must add number base you want, and thats 10, not 8 (for octal), so call:
parseInt("09", 10);
A.7. parseInt
parseInt is a function that converts a string into an integer. It stops when it sees a nondigit, so
parseInt("16") and parseInt("16 tons") produce the same result. It would be nice if the
function somehow informed us about the extra text, but it doesn't.
If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and
9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes
problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that
parseInt("08", 10) produces 8. I recommend that you always provide the radix parameter.
"JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc.,
978-0-596-51774-8."
this the reasons:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
http://www.w3schools.com/jsref/jsref_parseInt.asp
Do
parseInt("09", 10)
To specify base 10.
Edit: To clarify, a number prefixed by a zero will be assumed to be of octal notation, and "09" isn't a valid octal number.