JavaScript Program to Find Square Root of a Number
Use
Math.sqrt(number) in JavaScript to find the square root of a number, for example, Math.sqrt(16) returns 4.Examples
Input16
Output4
Input25
Output5
Input0
Output0
How to Think About It
To find the square root of a number, think about what number multiplied by itself gives the original number. We use the built-in
Math.sqrt() function in JavaScript which does this calculation for us easily.Algorithm
1
Get the input number from the user or code.2
Use the built-in function to calculate the square root.3
Return or print the result.Code
javascript
const number = 16; const squareRoot = Math.sqrt(number); console.log('Square root of', number, 'is', squareRoot);
Output
Square root of 16 is 4
Dry Run
Let's trace the example where number = 16 through the code
1
Assign number
number = 16
2
Calculate square root
squareRoot = Math.sqrt(16) = 4
3
Print result
Output: 'Square root of 16 is 4'
| Step | Variable | Value |
|---|---|---|
| 1 | number | 16 |
| 2 | squareRoot | 4 |
| 3 | output | 'Square root of 16 is 4' |
Why This Works
Step 1: Using Math.sqrt()
The Math.sqrt() function is built into JavaScript and returns the square root of the given number.
Step 2: Input and output
You provide a number as input, and it returns the positive square root as output.
Step 3: Printing the result
We use console.log() to show the result on the screen.
Alternative Approaches
Exponentiation operator
javascript
const number = 16; const squareRoot = number ** 0.5; console.log('Square root of', number, 'is', squareRoot);
This uses the power operator to find the square root by raising the number to 0.5 power; it's simple but less explicit than Math.sqrt().
Custom function using binary search
javascript
function sqrt(num) { if (num < 0) return NaN; let start = 0, end = num, mid; while (end - start > 0.00001) { mid = (start + end) / 2; if (mid * mid > num) end = mid; else start = mid; } return start; } console.log('Square root of 16 is', sqrt(16));
This method approximates the square root using binary search; useful if you want to avoid built-in functions but is more complex and slower.
Complexity: O(1) time, O(1) space
Time Complexity
Using Math.sqrt() is a constant time operation because it directly computes the result without loops.
Space Complexity
The operation uses constant space as it only stores a few variables.
Which Approach is Fastest?
Math.sqrt() is fastest and most efficient; exponentiation is also fast but less clear; custom methods like binary search are slower and more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Math.sqrt() | O(1) | O(1) | Simple and fast square root calculation |
| Exponentiation operator | O(1) | O(1) | Quick alternative but less explicit |
| Binary search method | O(log n) | O(1) | Learning or custom approximation without built-ins |
Use
Math.sqrt() for a quick and reliable way to find square roots in JavaScript.Trying to find the square root by multiplying or dividing instead of using
Math.sqrt() or exponentiation.