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 2 years ago.
when I try to get arguments starting with 0, like this case here 012 it is transferring it to 010 why is that can someone explain?
function func1(a, b, c) {
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
}
func1(1, 012, 3);
MDN screenshot
In JS the leading 0 converts the given number in octal base so 012 = (12)8 = (10)10
In Javascript a number starting with 0 is interpreted as octal.
BUT the console log is showing it to you in decimal.
012 = 1*8 + 2 in decimal which is 10.
Related
This question already has answers here:
How do you convert numbers between different bases in JavaScript?
(21 answers)
Closed 5 months ago.
I have some numbers (base 10) that I want to convert to a 32 bits(base 2), I have tried a lot of things, I found out the >>> operator, but apparently it only converts negative numbers to a base 10 the equivalent of the 32 bits, instead of base 2
const number = 3
const bitNumber = 1 >>> 0
console.log(bitNumber) /// 1
The numbers are always stored as bits internally. It’s console.log that converts them to a string, and the string conversion uses decimal by default.
You can pass a base to Number.prototype.toString:
console.log(bitNumber.toString(2));
and display as many bit positions as you want:
console.log(bitNumber.toString(2).padStart(32, '0'));
This question already has answers here:
How does using XOR to find a single element with odd number of occurrences in an array work?
(4 answers)
Closed 3 years ago.
I found out recently that you can use this function on an array of integers to find the one number that appears an odd number of times using JavaScript.
array.reduce((a, b) => a ^ b);
[1, 2, 2].reduce((a, b) => a ^ b); // returns 1
I'm struggling to figure out how the XOR operation (^) works and am wondering if there is a similar function that can find a number that appears an even number of times in an array.
XOR is a bit-wise operation where either of the comparison bits need to be 1 and the other 0 to result in a truthy value 1 (otherwise that's 0).
e.g.
10^12 =
10 = 1010
12 = 1100
----
0110 = 6
----
XOR Rules:
1 ^ 1 = 0 ^ 0 = 0
1 ^ 0 = 0 ^ 1 = 1
So, XOR of a number with itself gives 0. (Try it out.)
XOR of a number with 0 gives the number itself.
That means you're cancelling out all numbers which appear an even number of times. That leaves anything which does not occur an even number of times, XOR'ed with each other.
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:
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:
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.