JavaScript Program to Find the Square Root (5 Ways)
A square root of a number is a value that, when multiplied by itself, gives the original number. For instance, the square root of 16 is 4 because 4 * 4 = 16. In JavaScript, the Math.sqrt() function makes it simple to compute the square root. Let’s explore how to find square root in JavaScript with practical examples.
JavaScript Program to Find the Square Root Using Math.sqrt() Function
The Math.sqrt() function is the most common and direct way to calculate the square root in JavaScript.
Code
// JavaScript program to find the square root
let number = 16;
let squareRoot = Math.sqrt(number);
console.log("The square root of " + number + " is: " + squareRoot);
Output
The square root of 16 is: 4
Explanation
The Math.sqrt() function takes a number as input and returns its square root. In this example, the square root of 16 is calculated and displayed using console.log().
JavaScript Program to Find the Square Root Finding Square Root of User Input
You can make the program interactive by allowing the user to input a number.
Code
// How to find the square root in JavaScript with user input
let number = parseFloat(prompt("Enter a number:"));
if (number >= 0) {
let squareRoot = Math.sqrt(number);
alert("The square root of " + number + " is: " + squareRoot);
} else {
alert("Please enter a non-negative number.");
}
Output
The program prompts the user to enter a number and displays its square root in an alert box.
Explanation
Here, the prompt() function is used to take user input. The program checks if the input is non-negative before calculating the square root using Math.sqrt(). If the input is negative, an alert is displayed to guide the user.
JavaScript Program to Find the Square Root Using a Function for Reusability
You can encapsulate the logic in a reusable function.
Code
// JavaScript math square root using a function
function findSquareRoot(number) {
if (number >= 0) {
return Math.sqrt(number);
} else {
return "Invalid input. Enter a non-negative number.";
}
}
let result = findSquareRoot(25);
console.log("The square root is: " + result);
Output
The square root is: 5
Explanation
The findSquareRoot function accepts a number, validates it, and returns the square root using Math.sqrt(). If the input is invalid, it returns an error message.
JavaScript Program to Find the Square Root Using HTML for Interactive Input
This method demonstrates how to create a program that accepts user input from a form and calculates the square root.
Code
<!DOCTYPE html>
<html>
<body>
<h3>Find Square Root</h3>
<form>
<label for="number">Enter a number:</label>
<input type="text" id="number">
<button type="button" onclick="calculateSquareRoot()">Calculate</button>
</form>
<p id="result"></p>
<script>
function calculateSquareRoot() {
let number = parseFloat(document.getElementById("number").value);
if (number >= 0) {
let squareRoot = Math.sqrt(number);
document.getElementById("result").innerHTML = "The square root is: " + squareRoot;
} else {
document.getElementById("result").innerHTML = "Please enter a non-negative number.";
}
}
</script>
</body>
</html>
Output
The square root is displayed on the webpage after clicking the button.
Explanation
The calculateSquareRoot function retrieves the value entered in the input field, checks if it’s valid, and displays the square root in a paragraph element. This method is ideal for web-based applications.
JavaScript Program to Find the Square Root Square Root of Different Data Types
JavaScript is a dynamically typed language, and the Math.sqrt() function can handle various data types. Here’s how it behaves with different inputs:
Code
// JavaScript program to find the square root of different data types
console.log(Math.sqrt(9)); // Input: Integer
console.log(Math.sqrt(9.25)); // Input: Float
console.log(Math.sqrt("16")); // Input: String containing a number
console.log(Math.sqrt("text")); // Input: Non-numeric String
console.log(Math.sqrt(-4)); // Input: Negative Number
Output
3
3.0413812651491097
4
NaN
NaN
Explanation
-
For integers and floats, Math.sqrt() returns the correct square root.
-
For strings containing numeric values (e.g., "16"), JavaScript converts the string to a number and calculates the square root.
-
For non-numeric strings (e.g., "text"), Math.sqrt() returns NaN (Not a Number).
-
For negative numbers, Math.sqrt() also returns NaN since the square root of a negative number is undefined in real numbers.