Why does "1" - - "1" return 2? [duplicate] - javascript

This question already has an answer here:
Why does alert("1" - - "1"); produce 2 in javascript?
(1 answer)
Closed 8 years ago.
Why does alert("1" - - "1") return 2?
I'm not entirely sure what is going on here to create the result as 2?
I came across the problem here:
http://davidshariff.com/js-quiz

it's like writing 1 - (-1) which is 1 + 1 = 2
the problem in js is that if you use + between 2 string it means concating them thus resulting 11
EDIT: thanks to iamnotmaynard comment i was able to find this post
Why does JavaScript handle the plus and minus operators between strings and numbers differently?

Related

What does number + "e+4" mean in javascript [duplicate]

This question already has answers here:
'e' in javascript numbers
(3 answers)
Closed last month.
I have a code snippet with the following declaration:
const x = x + "e+4"
I dont understand what the "e+4" is and/or does.
This is a scientific notation.
e+4 is the same as writting * 10 ^ (+4)

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.

What is does (0 & 1) do in JavaScript? [duplicate]

This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
I am looking through the source of a library I'm working with, and I found something I haven't seen before.
$(item).each(function(child) {
oddEven = (i & 1);
targetNode.append(jasper_build_product(this,oddEven));
i++;
});
Notice the oddEven = (i & 1);. What does the (i & 1) part do? I'm particularly curious about the ampersand.
The & operator is a bitwise AND, and more specifically the expression x & 1 returns the least significant bit (LSB) of the value x.
Since the internal representation of numbers is base-2, an LSB of 1 indicates an odd value, and 0 indicates an even value.

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

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);

Categories