Question #53

Author: admin
tags: JavaScript  
console.log(-3 ** 3);
What will the console output be?
27
-27
9
-9
Nothing. There will be an error.
This is a curious task for those who are interested in how JavaScript works.
The minus sign here is the unary negation operator, and not at all part of the negative number literal, and not even the subtraction operator at all.
In the -3, the literal of the number is only the digit 3, and to the left of the number is the operator unary negation.
FireFox will write for the line from the task:
SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'
And Chrome will write for such a code line:
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
A similar error would be when using the unary plus.
To raise the "negative literal" to the power (although in reality it is already an expression, not a literal), you need to use parentheses:
console.log( (-3) ** 3 ); // -27
By the way, unary minus has priority 17, exponentiation has priority 16, and subtraction has priority 14.
Rate the difficulty of the question:
easyhard