The result of the above operation is 2. If you omit the space in the middle, an error will be reported. If there are spaces, the result will be 2. Can someone please analyze why?
When performing a subtraction operation, js will try to convert the expressions on both sides into numbers so that the operation can be carried out (a small error correction behavior), and become 1-(-1), which results in 2. As for the error reported without spaces, it is completely a grammatical error. . And this is an error, not a matter of constants or not. Even if it is changed to a variable, a--b is wrong syntax. The parser doesn't even know how to correct the error. If it is changed to a-(-b) for you Is it? That would directly destroy -- and destroy the logic. Unless you do a---b, the parser can make it (a--)-b for you, but this way of writing is also very funny. Maybe what you want to express is a-(--b)?
The parser will only make some corrections for you when the meaning (it thinks) is relatively clear, just like ''-'' only numbers in js support subtraction, so if you are not a number it will convert you into a number, but ''+'' and 0+'', in this case it needs to be corrected based on your first expression, otherwise it will not know whether you are doing string concatenation or addition. As for changing - into - - , that would be too bad, it is completely destructive error correction, and it definitely cannot be done.
Don’t get too hung up on this kind of thing, it’s an entertainment question. . When it comes to actually coding, I believe no one in their right mind would do this
When there is a space between two -, it is considered to be a minus sign, as @ Die Fatty answered. The strings on both sides will be converted into numbers and 1 minus -1 will be 2;
The two -没有空格,就变成了--without spaces become the -- operator, which is a self-decrement operation and can only be used for variables. Applying this notation to a number will definitely result in an error. If you don’t believe it, you can try it
1-- //Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
--1 //Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
First there are two operators, the space operator, the minus operator and the negative value operation. The latter has a higher priority than the former. -"1" will be implicitly converted to -1 Besides, there is no space, which is a self-decrement operation. As @deepgoing said, "constants" cannot perform auto-increment and self-decrement operations
When performing a subtraction operation, js will try to convert the expressions on both sides into numbers so that the operation can be carried out (a small error correction behavior), and become 1-(-1), which results in 2.
As for the error reported without spaces, it is completely a grammatical error. .
And this is an error, not a matter of constants or not. Even if it is changed to a variable, a--b is wrong syntax. The parser doesn't even know how to correct the error. If it is changed to a-(-b) for you Is it? That would directly destroy -- and destroy the logic.
Unless you do a---b, the parser can make it (a--)-b for you, but this way of writing is also very funny. Maybe what you want to express is a-(--b)?
The parser will only make some corrections for you when the meaning (it thinks) is relatively clear, just like ''-'' only numbers in js support subtraction, so if you are not a number it will convert you into a number, but ''+'' and 0+'', in this case it needs to be corrected based on your first expression, otherwise it will not know whether you are doing string concatenation or addition. As for changing - into - - , that would be too bad, it is completely destructive error correction, and it definitely cannot be done.
Don’t get too hung up on this kind of thing, it’s an entertainment question. . When it comes to actually coding, I believe no one in their right mind would do this
When there is a space between two
-
, it is considered to be a minus sign, as @ Die Fatty answered. The strings on both sides will be converted into numbers and 1 minus -1 will be 2;The two
-
没有空格,就变成了--
without spaces become the--
operator, which is a self-decrement operation and can only be used for variables. Applying this notation to a number will definitely result in an error. If you don’t believe it, you can try it1- -1 is equivalent to 1-(-1)
-"1" is a unary operation, which converts the string into -1. Compared to 1-(-1), we get 2.
If there are no spaces, the parser will report a syntax error.
First there are two operators, the space operator, the minus operator and the negative value operation. The latter has a higher priority than the former. -"1" will be implicitly converted to -1
Besides, there is no space, which is a self-decrement operation. As @deepgoing said, "constants" cannot perform auto-increment and self-decrement operations