
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Square root function without using Math.sqrt() in JavaScript
We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const square = (n, i, j) => { let mid = (i + j) / 2; let mul = mid * mid; if ((mul === n) || (Math.abs(mul - n) < 0.00001)){ return mid; }else if (mul < n){ return square(n, mid, j); }else{ return square(n, i, mid); } } // Function to find the square root of n const findSqrt = num => { let i = 1; const found = false; while (!found){ // If n is a perfect square if (i * i === num){ return i; }else if (i * i > num){ let res = square(num, i - 1, i); return res; }; i++; } } console.log(findSqrt(33));
Understanding the code
We looped over from i = 1. If i * i = n, then we returned i as n is a perfect square whose square root is I, else we find the smallest i for which i * i is just greater than n.
Now we know the square root of n lies in the interval i – 1 and i.
And then we used the Binary Search algorithm to find the square root.
Output
The output in the console will be −
5.744562149047852
- Related Articles
- How to perform square root without using math module in Python?
- Check for perfect square without using Math libraries - JavaScript
- Get minimum number without a Math function JavaScript
- Finding square root of a number without using Math.sqrt() in JavaScript
- Finding square root of a number without using library functions - JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Math. fround() function in JavaScript
- Math. hypot() function in JavaScript
- Program to check number is perfect square or not without sqrt function in Python
- Check if a number is perfect square without finding square root in C++
- Implementing Math function and return m^n in JavaScript
- JavaScript: How to Find Min/Max Values Without Math Functions?
- Reverse numbers in function without using reverse() method in JavaScript
- Square and Square root in Arduino
- Converting number of corresponding string without using library function in JavaScript

Advertisements