I somewhere saw the below code.
var num = "1" - - "1";
console.log(num);
The output is 2. Somebody explain me what is exactly happening here.
Thanks in advance.
I somewhere saw the below code.
var num = "1" - - "1";
console.log(num);
The output is 2. Somebody explain me what is exactly happening here.
Thanks in advance.
When using math operation(except the +
which used to string concatenation) for string, string will try to convert to number.
one minus (minus one) is two.
"1" - - "1"
is same as 1 - -1
which is 1 + 1
This is a conversion of both to numbers, and adding them. In javascript, - "1"
is actually just -1
, so "1" - - "1"
is 1 + 1
since it converts both into numbers. Note that if you do "1" + "1"
javascript treats it as concatenation and would give "11"
In Javascript:
"1" + 1 = "1" + "1" = 1 + "1" = "11"
-1 + "1" = "-11"
1 + "-1" = "1-1"
which is why math is so weird in javascript, but subtraction converts to numbers (since subtracting strings doesn't make sense.)
Subtracting a negative number is the same thing as adding the absolute value of that number. This is a common property of math, not of javascript. As written, the code is of course silly. It may be the result of a programmer not familiar with parseInt
, trying to avoid an issue where the plus sign is interpreted as string concatenation.
1 - - 1 == 1 + 1 // true
"1"- -"1" == "1" + "1" // false
"1" - - "1" //as "-" is an arithmetic operator
(numeric equivalent of "1"-> parseInt( "1", 10 )) - - ( numeric equivalent of "1" )
= 1 - - 1
= 1 - (-1)
= 2
This is the result of JavaScript's type coercion methodology.
In expressions involving numeric and string values with the
+
operator, JavaScript converts numeric values to strings.In statements involving other operators, JavaScript does not convert numeric values to strings.
This quote is for the opposite case (converting numeric values to strings, but the same rules might apply).
Another example given is (+"1.1") + (+"1.1") = 2.2
. In this case, the parentheses cause JavaScript to bind the +
operator to the strings as the unary +
operator, which thereby converts the strings to numeric types.
Read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
Since your example involves "other operators", JavaScript appears to bind the -
operator as the unary negative, which coerces the strings to numeric types before doing the computation (which has the effect of adding the numeric values represented in the strings).
Take 1 and take away -1, it's the same as adding 1.
In other words
1 - (-1)
= 1 + 1
= 2
This happens because double negative is positive (short video explanation, long explanation, logical concept of double negation) and with "the help" of implicit data type conversion - in javascript, our case.
So,
1 - - 1 // = 2
but
1 - - - 1 // = 0, weird but it works :-)
Why? Because every even number of '-' signs gets evaluated to '+', and every odd number of '-' signs gives '-', when operations are made on numbers. Given these, in the first example above "1 - - 1 = 2" we have 2 minus signs, so the expression is really "1 + 1 = 2", which is correct. In the second example, we have 3 minus signs, the expression become "1 - 1 = 0", which is correct also.
Using the same principle, someone (a dark dark overlord maybe :-), could write something along these lines, just for confusing purposes:
var a = 3 + - + - + - + - + - - - - - + + + + + - - - + + + 1
and would ask if this gives an error? Using the same algorithm above, we can say our "expression" has 12 "-" signs, 12 being an even number => all these "-" signs return "+", so basically, we can rewrite the assignment above:
var a = 3 + 1 // which gives 4
Other weird examples:
- - - - + + + - + + + - - - + + - + + - - 3 // returns -3
- - - - + + + - + + + - - - + + - + + - - - - - + + 3 // returns 3
- + - - - - new Date() // return current negative timestamp
- (+ - - -(function() { return 3; })()) // returns 3
var a = - + - 1, b = - - (- - - a); a + b // returns 0
Info: No dark overlords were harmed during the writing of this comment :-).