OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JavaScript exponentiation unary operator design decision

  • Thread starter Thread starter kemotoe
  • Start date Start date
K

kemotoe

Guest
So I was fooling around with the new exponentiation operator and I discovered you cannot put a unary operator immediately before the base number.

Code:
let result = -2 ** 2; // syntax error
let result = -(2 ** 2); // -4
let x = 3;
let result = --x ** 2; // 4

From the documentation on MDN:

In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (+/-/~/!/delete/void/typeof) immediately before the base number.

In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or **), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators.

I understand this was made an error by design. I don't understand this design decision. Who's really going to be surprised that -x ** 2 is negative? This follows not only other mainstream programming languages but a mathematical notation that has been in common use for hundreds of years and is taught to every high school algebra student.

In Javascript '1'+ 2 is '12' and '1'-2 is -1 but -1**2 raises an error because it could be ambiguous? Help me understand this design decision.

<p>So I was fooling around with the new exponentiation operator and I discovered you cannot put a unary operator immediately before the base number.</p>
<pre><code>let result = -2 ** 2; // syntax error
let result = -(2 ** 2); // -4
let x = 3;
let result = --x ** 2; // 4
</code></pre>
<p>From the <a href="https://developer.mozilla.org/en/do...tors/Arithmetic_Operators#Exponentiation_(**)" rel="noreferrer">documentation on MDN</a>:</p>
<blockquote>
<p>In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (<code>+</code>/<code>-</code>/<code>~</code>/<code>!</code>/<code>delete</code>/<code>void</code>/<code>typeof</code>) immediately before the base number.</p>
<p>In most languages like PHP and Python and others that have an exponentiation operator (typically <code>^</code> or <code>**</code>), the exponentiation operator is defined to have a higher precedence than unary operators such as unary <code>+</code> and unary <code>-</code>, but there are a few exceptions. For example, in Bash the <code>**</code> operator is defined to have a lower precedence than unary operators.</p>
</blockquote>
<p>I understand this was made an error by design. I don't understand this design decision. Who's really going to be surprised that <code>-x ** 2</code> is negative? This follows not only other mainstream programming languages but a mathematical notation that has been in common use for hundreds of years and is taught to every high school algebra student.</p>
<p>In Javascript <code>'1'+ 2</code> is <code>'12'</code> and <code>'1'-2</code> is <code>-1</code> but <code>-1**2</code> raises an error because it could be ambiguous? Help me understand this design decision.</p>
 

Latest posts

I
Replies
0
Views
1
Ihsan Sadat
I
Top