Home Javascript JavaScript pow() method
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JavaScript pow() method

Advertisement
Some times programmers may need to evaluate the value by the power of some numbers therefore JavaScript provides an in-built function Math.pow().

JavaScript pow() method

     

Some times programmers may need to evaluate the value by the power of some numbers therefore JavaScript provides an in-built function Math.pow().

Syntax:

Math.pow(x,y);

Math.pow(x,y) returns value of x to the power of y (xy). Where x and y both are required.

In the following example we will describe you the use of Math.pow() method with very simple code. Here in this example code we have created two input boxes into which we will take the values of x and y from the user side.

Description of code:

Take two values x and y from the user and then apply the Math object's method pow() onto it. When user clicks on the button "Show Value" it calls the method callPow() as defined in the JavaScript within our <script></script> tag.

 function callPow(){
   var val= document.getElementById("txt").value;
   var power= document.getElementById("txt2").value;
	alert(Math.pow(val,power));
 }

Here we have taken the values of x and y into the "val" and "power" variables. Full html code for this example is as follows :

<html>
<body>
 <script>
 function callPow(){
   var val= document.getElementById("txt").value;
   var power= document.getElementById("txt2").value;
	alert(Math.pow(val,power));
 }
 </script>
 <div style="background: #cf2255; width:'100%';"
              align="center">
  <font color="#ffffcc" size="12pt">
     <b>Pow Example</b></font>
 </div>
  <center>
  <div style="background: #ffffcc; 
  width:'100%';" align="center">
   Input number :<input type="text" id="txt" value=""/>
    to the power<input type="text" id="txt2" value=""/>
  <button onclick="callPow();">Show Value</button>
  </div>
  </center>
</body>
</html>

Output :

Input numbers into the text boxes :

then click on button "Show Value" to view results.

Calculate 3 to the power 5 (35).

You can also download full HTML code from the following link as given below.

Download Source Code

     

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 7, 2009

Ask Questions?    Discuss: JavaScript pow() method  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Binny
May 26, 2011
Nice

Really helped a lot.. thanks :)...keep it up for more things
Tony
January 3, 2012
POW

Hello, I am not good in programming. I have the input number only 2, and the POW number like 3 (or up to 13) but the POW number comes with A and B,and I know the Value is 8, but the thing I want it to list like: POW = 3 ( The line list will depend on the POW number): 1A, 2A, 3A; 1A, 2A, 3B; 1A, 2B, 3A; 1A, 2B, 3B; etc.... Your help will be very grateful Regards,
DMCA.com