Find Square Root of a Number Program
In this tutorial, you are given a number. Write a JavaScript program with a function that takes the number as argument, finds the square root of this number, and returns the result.
Solution
To find the square root of a number in JavaScript, we can use Math.sqrt()
method.
Programs
1. In the following program, we take a number in n
, find its square root, and print the result to console output.
function findSquareRoot(num) {
return Math.sqrt(num);
}
var n = 5;
const result = findSquareRoot(n);
console.log(result);
Math.sqrt()
function is used in the above program. JavaScript Math library contains trivial functions related to mathematics.