How does Javascript get this value? [duplicate] - javascript

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.

Related

arguments starting with 0 giving different numbers than intended in 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 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.

How to write negative binary number? [duplicate]

This question already has an answer here:
ECMAScript 6 negative binary literal
(1 answer)
Closed 3 years ago.
So 0b1 is 1. How to write -1 in binary format?
The docs says that the leftmost bit is reserved for sign, but where is that leftmost bit? Even 0b11111111111111111111111111111111 is still a positive number. I naively tried 1b with no success of course.
try
let a = -0b1;
console.log(a);

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 Leading 0 on Integers Wrong Value [duplicate]

This question already has answers here:
Javascript 0 in beginning of number
(3 answers)
Closed 5 years ago.
Javascript leading 0 on integers getting wrong value on console.log.
Why I am getting like this ?
Code:
console.log(456);
console.log(0456);
Output:
456
302
Because JS "translates" 0456 as an octal value, since it has a trailing zero and all its digits < 8.

Can someone explain me Javascript operators - why this output? [duplicate]

This question already has an answer here:
Javascript integer beginning with zero [duplicate]
(1 answer)
Closed 5 years ago.
I have created a calculator but it has such problem
var x=060;
var y=60;
console.log(x+y);
The output is 108 why so?
And, How can i make calculator to enter single operator and wait for an operand and then an operator could appear in.
This is my calculator
Calculator2.0
Put 0 in front of a number means the base is octal. In your case 060 (octal) = 48 (decimal). You add 60 + 48 that is equal to 108.

Categories