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

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.

Related

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

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.

How does Javascript get this value? [duplicate]

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.

Converting Decimal Number to Binary [duplicate]

This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
How do I convert an integer to binary in JavaScript?
(17 answers)
Closed 5 years ago.
I found this code on W3schools for converting decimal into binary.
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
I don't understand what is the purpose for making zero fill right shift in this case? What is the use of setting the shift to 0 (i.e. dec >>> 0) since as I know it doesn't push any binary digit to the right at all?
I am a JS beginner. Thanks!

What does n%7 mean? [duplicate]

This question already has answers here:
What does % do in JavaScript?
(10 answers)
Closed 7 years ago.
When I was repairing a Project via Java that I did about a year ago I came across a piece of Code using n%7 and its a mystery to me what it does. What I understand what it exactly means - Not referring to %.
Thanks in advance,
DR
It is called modulus. It can be read as n modulus 7.
The MODULUS Operator %
The modulus operator finds the modulus of its first operand with respect to the second. That is, it produces the remainder of dividing the first value by the second value. For example:
22 % 6 = 4 because 22 / 6 = 3 with a remainder of 4
read more here: http://mathbits.com/MathBits/Java/DataBasics/Mathoperators.htm

Categories