In JavaScript, what do `void 2 === "2"` and `2 ** 3 ** 2` evaluate to, and why is `-2 ** 2` a SyntaxError?
answer
- grouping is not evaluation order
- prefix operators bind before binary ones
- exponent groups from the right
- ambiguity the language refuses to resolve
- parentheses on either side fix it
basics
~20 svoid 2 === "2" is false because void binds first, so undefined is compared with "2". 2 ** 3 ** 2 is 512 because ** is right-associative. -2 ** 2 is rejected as ambiguous, so the grouping must be parenthesized.
solid answer
~50 sPrecedence decides which operator owns an operand, and associativity only breaks ties among operators at the same level. Prefix operators such as `typeof` and `void` bind tighter than every binary operator, so `void 2 === "2"` is `(void 2) === "2"`, which is `false`, and `typeof 1 + 2` is `"number2"` rather than the type of the sum. `**` is right-associative, so `2 ** 3 ** 2` is `2 ** 9`, or `512`, while the other arithmetic operators are left-associative. `-2 ** 2` is special: languages disagree on whether it means `-4` or `4`, so JavaScript forbids a unary operator directly before the base of `**`, and the script fails to parse with a `SyntaxError`. You write `-(2 ** 2)` or `(-2) ** 2`. None of this changes evaluation order, because operands still run left to right. In review I add parentheses wherever a reader would otherwise need the table.
code
javascript · 9 linesconsole.log(typeof 1 + 2); // "number2"
console.log(typeof (1 + 2)); // "number"
console.log(void 2 === "2"); // false
console.log(void (2 === "2")); // undefined
console.log(2 ** 3 ** 2); // 512
console.log((2 ** 3) ** 2); // 64
console.log(-(2 ** 2)); // -4
console.log((-2) ** 2); // 4
console.log(2 ** -2); // 0.25go deeper
Know that typeof and void apply only to what comes right after them, that ** groups from the right, and that -2 ** 2 does not run at all, so parentheses are needed.
Separate precedence from associativity from evaluation order, and predict results such as 2 ** 3 ** 2, typeof 1 + 2 and 3 > 2 > 1 by showing the implied parentheses rather than by recalling a table.
Explain why the language rejects -2 ** 2 rather than picking a meaning, and use parentheses as a readability rule, so nobody needs the table. Catch shifts and masks mixed with arithmetic or comparison.
A team's choice is how far to let a formatter and lint rules add explicit parentheses to mixed operators, trading a little visual noise for expressions that nobody has to decode during a review or an incident.
## Two separate rules Given `a OP1 b OP2 c`, the parser must decide who owns `b`. - **Precedence** answers this when the two operators sit at different levels: the higher one takes the operand. - **Associativity** answers it when they sit at the same level. Left-associative means `(a OP1 b) OP2 c`, and right-associative means `a OP1 (b OP2 c)`. These rules determine grouping and nothing else. **Operands still evaluate left to right**, whatever the grouping says. ## The levels worth remembering | Level | Operators | Associativity | |---|---|---| | 14 | prefix `!`, `~`, `+`, `-`, `typeof`, `void`, `delete`, `await` | not applicable | | 13 | `**` | right to left | | 12, 11 | `*`, `/`, `%`, then `+`, `-` | left to right | | 10 | `<<`, `>>`, `>>>` | left to right | | 9, 8 | `<`, `>`, then `==`, `===` | left to right | | 7, 6, 5 | `&`, then `^`, then `\|` | left to right | | 2 | `=` and the compound assignments | right to left | | 1 | the comma operator | left to right | Three facts cover most review traps: prefix operators bind very tightly, shifts sit below arithmetic, and the bitwise operators sit below comparison. ## Prefix operators A prefix operator takes the smallest thing to its right, so anything wider has to be parenthesised: - `typeof 1 + 2` is `(typeof 1) + 2`, the string `"number2"`. `typeof (1 + 2)` is `"number"`. - `void 2 === "2"` is `(void 2) === "2"`, which compares `undefined` with `"2"` and gives `false`. `void (2 === "2")` gives `undefined`. - `~1 + 1` is `(~1) + 1`, which is `-1`. - `!1 + 1` is `(!1) + 1`, and since `false` converts to `0`, the result is `1`. ## `void` as an operator The `void` operator evaluates its operand, discards the result and produces `undefined`. Its operand still runs, so `const r = void (n = 5);` leaves `n` at `5` and `r` undefined. (The `void 0` spelling of `undefined` belongs with the primitive values.) Its useful forms all come from that discard: ```javascript const save = () => false; const leaky = () => save(); const quiet = () => void save(); console.log(leaky(), quiet()); // false undefined ``` An arrow function with an expression body returns that expression, so `button.onclick = () => save();` would hand the return value to the event system, where `false` cancels the default action. `void` makes the discard explicit. It can also force expression position for an immediately invoked function, as in `void function () { /* ... */ }();`. That works only with the `function` keyword: `void () => {}();` is a `SyntaxError`. ## Exponentiation breaks two expectations **It is right-associative.** `2 ** 3 ** 2` is `2 ** (3 ** 2)`, so `512`, while `(2 ** 3) ** 2` is `64`. **A unary operator cannot sit directly before its base.** Languages split on whether `-2 ** 2` means `-4` or `4`, so JavaScript refuses to choose: 1. `-2 ** 2` is an early `SyntaxError`, so the whole script fails to load. 2. `typeof 2 ** 2`, `void 0 ** 2`, `!2 ** 2`, `~2 ** 2` and `await x ** 2` fail in the same way. 3. `-(2 ** 2)` is `-4`, and `(-2) ** 2` is `4`. 4. `2 ** -2` is fine and gives `0.25`, because the ban concerns the base, not the exponent. ## Grouping is not evaluation order Right-associativity does not make the right operand run first: ```javascript const echo = (name, n) => (console.log(name), n); console.log(echo("left", 4) ** echo("middle", 3) ** echo("right", 2)); // left, middle, right, then 262144 ``` The calls run left to right, and only then is `3 ** 2` computed, followed by `4 ** 9`. ## Other traps in review - `3 > 2 > 1` is `false`: it groups as `(3 > 2) > 1`, which is `true > 1`, and `true` converts to `1`. - `1 + 2 << 1` is `6`, and `1 << 2 + 1` is `8`, because addition binds first. - `1 | 2 << 1` is `5`, and `(1 | 2) << 1` is `6`. - `a = b = 5` works because assignment is right-associative and yields the assigned value. - `x & 1 === 1` groups as `x & (1 === 1)`, so parenthesise the mask.
- What does `void` do in `button.onclick = () => void save();`, and what would change without it?`void` runs `save()` and makes the arrow return `undefined` whatever `save` returns. Without it the arrow returns `save()`'s result, and if that ever becomes `false`, an event handler property cancels the default action. So `void` keeps a later change to `save` from altering the handler's behaviour.
- In `f() ** g() ** h()`, which call runs first, given that `**` is right-associative?`f()` runs first, then `g()`, then `h()`. Associativity decides only the grouping, `f() ** (g() ** h())`, so the second exponentiation is computed before the first. Operands are always evaluated from left to right, so only the arithmetic happens in the right-to-left order.
- Why does `3 > 2 > 1` evaluate to `false` in JavaScript?Relational operators are left-associative, so it groups as `(3 > 2) > 1`. The inner comparison gives `true`, then `true > 1` converts `true` to `1`, and `1 > 1` is `false`. JavaScript has no chained comparison, so write `3 > 2 && 2 > 1`.
- Is `2 ** -2` a SyntaxError as well?No. The restriction applies to a unary operator immediately before the base, because only that position is ambiguous. A unary operator on the exponent has one possible reading, so `2 ** -2` is valid and evaluates to `0.25`.
- -2 ** 2 evaluates to -4 in JavaScript, as it does in several languages
- ** is left-associative like *, so 2 ** 3 ** 2 is 64
- Right-associativity means the rightmost operand is evaluated first
- typeof 1 + 2 returns the type of the sum
- void applies to the whole comparison in void 2 === "2"
- Any unary operator near ** is forbidden, including in 2 ** -2