About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
Javascript is used by almost every website to do calculations on the browser and support client-side interaction as well as server-side computations. Here we will see how to write simple Javascript programs to compute powers of numbers.
We will embed our Javascript code inside a HTML page like so:
In the above HTML page you see the basic elements of the page like the head element containing the title (“Javascript power demo”) and a body with an empty script tag. The Javascript code goes inside these tags.
There are two ways to compute powers in Javascript. Below is each approach described in detail.
The first way to compute powers is to use the exponentiation operator (**). Here is some basic code for the same (only the script parts of the page are shown):
When this page is rendered, it will look like:
You can also use the “**=” exponentiation assignment operator like so:
This will also output:
A second method is to use the Math.pow() function. Here is how that looks (with a newline printed in HTML):
The output is:
In the Math.pow() function, the first argument is the base (2, in our case) and the second argument is the exponent (10, in our case).
Note that if the base is 0, the result of the exponentiation will be 0. Similarly, if the exponent is 0, the result of the exponentiation will be 1. Here is a simple program to try this:
The output is:
You can also use the Math.pow() function with negative (Integer) arguments:
The output is:
If we try a program where zero is raised to negative exponents:
Javascript outputs:
Let us try Math.pow() with floating point inputs for either the base or the exponent (or both):
The output will be:
Note that we get a NaN (Not a Number) output whenever the base is negative and the exponent is a floating point number.
Try the same input combinations above with the exponentiation operator and see if you get the same outputs!
Enjoy computing powers in Javascript!
Would you like to learn more Javascript? Checkout our blog post on checking if numbers are within a range in Javascript!
Want to learn Python with us? Sign up for 1:1 or small group classes.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Javascript power demo</title>
</head>
<body>
<script>
</script>
</body>
</html>
<script>
document.write("2^10 is: " + 2**10);
</script>
2^10 is: 1024
<script>
x = 2;
x **= 10;
document.write("2^10 is: " + x);
</script>
2^10 is: 1024
<script>
document.write("2^10 is: " + 2**10);
document.write("<BR>");
document.write("Another way to do 2^10 is: " + Math.pow(2,10));
</script>
2^10 is: 1024
Another way to do 2^10 is: 1024
<script>
document.write("2^0 is: " + Math.pow(2,0));
document.write('<BR>')
document.write("0^2 is: " + Math.pow(0,2));
</script>
2^0 is: 1
0^2 is: 0
<script>
document.write("2^-1 is: " + Math.pow(2,-1));
document.write('<BR>')
document.write("-1^2 is: " + Math.pow(-1,2));
</script>
2^-1 is: 0.5
-1^2 is: 1
<script>
document.write("0^-3 is: " + Math.pow(0,-3));
</script>
0^-3 is: Infinity
<script>
document.write("0.5^3.5 is: " + Math.pow(0.5,3.5));
document.write('<BR>')
document.write("-0.5^3.5 is: " + Math.pow(-0.5,3.5));
document.write('<BR>')
document.write("0.5^-3.5 is: " + Math.pow(0.5,-3.5));
document.write('<BR>')
document.write("-0.5^-3.5 is: " + Math.pow(-0.5,-3.5));
</script>
0.5^3.5 is: 0.08838834764831843
-0.5^3.5 is: NaN
0.5^-3.5 is: 11.31370849898476
-0.5^-3.5 is: NaN