Why is parseInt(021, 8) === 15? [duplicate] - javascript

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');

Related

Why does a JavaScript function automatically convert a binary number when I pass it as an argument? [duplicate]

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.

Javascript Implicit Numbers Conversion [duplicate]

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.

Bug in Javascript parseInt method? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
Parsing a string using parseInt method returns invalid output .
Code :
parseInt("08");
Excepted Output :
8
Real Output :
0
Code [This returns output correctly] :
parseInt("8")
Output :
8
Why it happens ?
You need to specify the base:
parseInt("08",10); //=>8
Otherwise JavaScript doesn't know if you are in decimal, hexadecimal or binary.
(This is a best practise you should always use if you use parseInt.)
Also see Number:
Number("08"); // => 8
What is the difference between parseInt() and Number()?
What is the difference between parseInt(string) and Number(string) in JavaScript?
You should tell parseInt its 10 based:
parseInt("08", 10);
JavaScript parseInt() Function
If the radix parameter is omitted, JavaScript assumes the following:
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://jsfiddle.net/YChK5/
Strings with a leading zero are often interpreted as octal values. Since octal means, that only numbers from 0-7 have a meaning, "08" is converted to "0". Specify the base to fix this problem:
parseInt("08", 10); // base 10
As usual, the MDN is a good source of information:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt#Octal_Interpretations_with_No_Radix

Understanding number bases in javascript [duplicate]

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.

alert(parseInt("09")); shows me "0" Why? [duplicate]

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.

Categories