• 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
  • VBA
  • About
You are here: Home / JavaScript / JavaScript Square Root with Math.sqrt() Method

JavaScript Square Root with Math.sqrt() Method

January 5, 2022 Leave a Comment

In JavaScript, to get the square root of a number we use the Math.sqrt() method.

var num = Math.sqrt(9);

The above code would return the number 3.

Some other examples of Math.sqrt() are below:

var num = Math.sqrt(1);
var num1 = Math.sqrt(0);
var num2 = Math.sqrt(10);
var num3 = Math.sqrt(196);
var num4 = Math.sqrt(31.3);
var num5 = Math.sqrt(-50);

Which would result in the following:

1
0
3.1622776601683795
14
5.594640292279746
NaN

JavaScript Square Root in action using jQuery

Below we will provide code to let the user input a number, and then use the Math.sqrt() method to find the square root of that number. Here is our simple HTML setup:

<p>Type a number you want to use the Math.sqrt() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

Below is the JavaScript and jQuery code which take the user input using the jQuery click() or on() keypress methods, and use the Math.sqrt() method on that user input and update the results below using the jQuery text() method.

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Math.sqrt($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.sqrt($("#userVal").val()));
});

The final code and output for this example is below:

Code Output:

Type a number you want to use the Math.sqrt() method on below:


Full Code:

<p>Type a number you want to use the Math.sqrt() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

<script>

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Math.sqrt($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.sqrt($("#userVal").val()));
});

</script>

Hopefully this article has been useful in helping you understand how to find the square root of a number in JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Iterate through an Array Using JavaScript
  • 2.  Using JavaScript to Sort an Array of Strings
  • 3.  Using JavaScript to Remove Apostrophe From a String
  • 4.  Using the appendChild Method with JavaScript to Add Items to a List
  • 5.  Remove Word From String In JavaScript
  • 6.  Using the getElementsByClassName Method in JavaScript
  • 7.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 8.  How to Remove Non Numbers From String in JavaScript
  • 9.  Using JavaScript to get the Last Element in Array
  • 10.  Adding an Underline with JavaScript

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy