• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Power Operator

JavaScript Power Operator

July 27, 2022 Leave a Comment

In JavaScript, the Power Operator, or exponentiation operator, **, is used to raise a number to the power of an exponent.

var num = 2**4;

The output of the code above would be the number 16.

Some other examples of the power operator are below:

var num1 = 6**1;
var num2 = 2**2;
var num3 = 3**25;
var num4 = 0**3;
var num5 = (-2)**2;
var num6 = (-2)**3;
var num7 = (-2)**-2;
var num8 = (-2)**-3;
var num9 = 3**0;
var num10 = .3**4;

This would result in the following:

6
4
847288609443
0
4
-8
0.25
-0.125
1
0.0081

One main thing to be aware of is that any negative base number will need to be in parentheses. So (-2)**2 and not -2**2.


Note that we can also use the Math.pow() method to get the same result.

var num = Math.pow(2,4);

The output of the code above would again be the number 16.

Let’s see the examples above using the Math.pow() method:

var num1 = Math.pow(6,1);
var num2 = Math.pow(2,2);
var num3 = Math.pow(3,25);
var num4 = Math.pow(0,3);
var num5 = Math.pow(-2,2);
var num6 = Math.pow(-2,3);
var num7 = Math.pow(-2,-2);
var num8 = Math.pow(-2,-3);
var num9 = Math.pow(3,0);
var num10 = Math.pow(.3,4);

They result in the same output as we had using the power operator above:

6
4
847288609443
0
4
-8
0.25
-0.125
1
0.0081

JavaScript Power Operator in Action

Below we will provide code to let the user input two numbers, and then use the power operator to find the first number(base) to the power of the second number(exponent). Here is our simple HTML setup:

<p>Enter two numbers below. We will provide the result of the first number to the power of the second number.</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="runFunction()">Submit</button>
<div id="results"></div>

Below we will write our function. We will get both numbers that the user has entered using the value property. We will then display the results using the textContent property.

function runFunction() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  if( num1 < 0 ){
    var num = (Number(num1))**Number(num2);
  } else {
    var num = Number(num1)**Number(num2);
  }
  document.getElementById("results").textContent = num;    
}

The final code and output for this example is below:

Code Output:

Enter two numbers below. We will provide the result of the first number to the power of the second number.



Full Code:

<p>Enter two numbers below. We will provide the result of the first number to the power of the second number.</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="runFunction()">Submit</button>
<div id="results"></div>

<script>

function runFunction() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  if( num1 < 0 ){
    var num = (Number(num1))**Number(num2);
  } else {
    var num = Number(num1)**Number(num2);
  }
  document.getElementById("results").textContent = num;    
}

</script>

Hopefully this article has been useful in helping you learn about JavaScript Power Operator.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get the Length of Array
  • 2.  How to Create a JavaScript Count Up Timer
  • 3.  JavaScript Capitalize First Letter of Every Word
  • 4.  Using JavaScript to Add Seconds to a Date
  • 5.  How to Convert Radians to Degrees Using JavaScript
  • 6.  Using JavaScript to Set the Width of a Div
  • 7.  How to Convert a String to Lowercase In JavaScript
  • 8.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 9.  Create Array of Zeros in JavaScript
  • 10.  Using JavaScript to Convert Float to Integer

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy