I don't understand why the following javascript parses; what's going on?
var a = 0;
var b = 0;
console.log(a + + + + + + + b);
console.log(a);
console.log(b);
In addition, when it does parse I'd at least expect one of a
or b
to be incremented, but they are not.
0
0
0
0
0
0
No, the increment way is with a++
.
That you make here is to changing the sign of the number, but we know that +
don't change the sign (opposite to -
). So in this:
console.log(a + + + + + + + b);
You are saying:
a + (+ (+ (+ (+ b) ) ) )
So you are not changing the sign nor incrementing the numbers. Check this:
console.log(a++ + + + + + + + b);
You'll obtain an increment in a
variable. Output:
0
1
0
Javascript has an unary plus operator, so this expression is essentially the same as
console.log(a + (+ (+ (+ (+ (+ (+ b)))))));
which is just, eventually, equivalent to
console.log(a + b);
Note that the increment operator, ++
(be it a prefix or postfix operator) cannot have a whitespace between the +
s.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.