- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain exponentiation operator in JavaScript?
The exponentiation operator (**) works on operands. First operand will behave as power to the second operand. It is quite similar to math.pow. Apart from these, this operator also accepts BigInts as operands.
This following example below will brief you
Let ans = 3 ** 3; document.write(ans); // 27
As mentioned in the above paragraph, This operator also accepts the numbers for BigInt. Consider this example below −
Let ans = 3n ** 3n; Document.write(ans); //27
Example 1
(a ** b)
This following example represent, usage of exponentiation operator (**). In this below scenario, we’ve considered two operands "a" and "b" and performed the operation.
<!DOCTYPE html> <html> <body> <title>Exponentiation Assignment (**)</title> <p id="demo"></p> <script> let a = 6; let b = a ** 2; // 6 is base operand and 2 is power operand. document.getElementById("demo").innerHTML = b; //36 let output = 3 ** 4; //3 is base operand and 4 is power operand. document.write(output, "<br>"); // 81 </script> </body> </html>
Example 2
Infix notation (**=)
The below example says that you can also use the exponentiation operator (**) in the infix notation. Let’s consider the below scenario
<!DOCTYPE html> <html> <body> <title>Exponentiation Assignment in the infix notation (**=)</title> <script> let a = 5; a **= 2; // 2 will be the base operand and 4 is power operand. document.write(a); // 25 </script> </body> </html>
Example 3
Ambiguous exponentiation expression
It is not possible to write an ambiguous exponentiation expression in JavaScript because if we try to write any unary operator straight away before the base operand, it will lead to Syntax error. Let’s consider the below example
<!DOCTYPE html> <html> <body> <title>Ambiguous exponentiation expression</title> <script> let output = -2**5; document.write(output,"<br>"); // This will show you syntax error. let outcome = (-2)**5; document.write(outcome); // -32 </script> </body> </html>
Output
The output of the above script will be −
-32
Note
Syntax error happened in the above example because, we have used the unary operator straight away before exponentiation expression. So, to avoid that syntax error we need to include parenthesis as done in above scenario.
Example 4
Consider this example, which are dealing with three operands.
<!DOCTYPE html> <html> <body> <title>Three operands</title> <script> let output_1 = 3**3**2 document.write(output_1,"<br>"); // 19683 let output_2 = 3**(3**2); document.write(output_2,"<br>"); // 19683 let output_3 = (3**3)**2; document.write(output_3,"<br>"); // 729 </script> </body> </html>
Note
Consider "3**3**2", here we start solving from left to right. 3 will be the base operand and second 3 will be the power operand, after solving these two operands the result value will be base operand and 2 will be power operand.
Let’s take this 3**(3**2), in this case we start solving from right to left, The 3 which is in parenthesis will act as base operand to 2, after solving the result will be power operand to the left over 3.
Coming to this scenario (3**3)**2, we start solving from left to right, The first 3 inside the parenthesis will act as base operand and other 3 will act as power operand. After solving the result value will act as base operand and 2 is power operand.
- Related Articles
- Explain Grouping operator in JavaScript.
- Explain about add and assignment(+=) operator in detail in javascript?
- Explain about logical not(!) operator in detail with example in javascript?
- Python program for Modular Exponentiation
- Explain function of % operator in Python.
- Instanceof operator in JavaScript
- Modular Exponentiation (Power in Modular Arithmetic) in java
- What is instanceof operator in Java? Explain.
- JavaScript Spread Operator
- The new operator in JavaScript
- Optional chaining operator in JavaScript.
- C++ Program to Implement Modular Exponentiation Algorithm
- Explain difference between == and is operator in Python.
- Explain Operator grammar and precedence parser in TOC
- Find Nth term (A matrix exponentiation example) in C++
