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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / JavaScript Exponent – Exponentiate Numbers with Math.pow()

JavaScript Exponent – Exponentiate Numbers with Math.pow()

February 4, 2022 Leave a Comment

In JavaScript, to get the value of a number and its exponent, we can use the Math.pow() method.

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

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

Some other examples of Math.pow() method are below:

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);

Which would result in the following:

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

Note that we can also use the exponentiation operator (**) to get the same results. Here is an example:

var num = 2**4;

The above code will give the same result as our first example of Math.pow(2,4) = 16.

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

JavaScript Exponents in Action

Below we will provide code to let the user input two numbers, and then use the Math.pow() method 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="text" value1="">
<input id="userVal2" type="text" 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;
  var num = Math.pow(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="text" value1="">
<input id="userVal2" type="text" 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;
  var num = Math.pow(Number(num1), Number(num2));
  document.getElementById("results").textContent = num;    
}

</script>

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

Other Articles You'll Also Like:

  • 1.  How to Clear a Textarea Field In JavaScript
  • 2.  How to Change an Image on Hover Using JavaScript
  • 3.  Reverse Words in a String JavaScript
  • 4.  JavaScript atanh – Find Hyperbolic Arctangent of Number
  • 5.  Check if Character is Uppercase in JavaScript
  • 6.  Using JavaScript to Check a Radio Button
  • 7.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 8.  JavaScript value – Get the Value from an Input Field
  • 9.  Using JavaScript to Capitalize the First Letter of a String
  • 10.  Using JavaScript to Declare Multiple Variables

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