Languages
[Edit]
EN

Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence - Google Chrome

1 points
Created by:
JustMike
3488

1. Problem

This code:

// ONLINE-RUNNER:browser;

console.log( -2 ** 4 ); // Syntax error!

Throws exception:

Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence

2. Solution

If base value is negative it should be placed in brackets.

In our case we have -2, so we put it into brackets, like this: ( -2 ) and it solves the problem.

Example:

// ONLINE-RUNNER:browser;

console.log( (-2) ** 4 ); // 16

See also

  1. JavaScript - Math.pow() method example
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.