Square root in JavaScript
In this post, you will learn how to write a program to find the square root of a number using the JavaScript programming language.
In mathematics, the square root of a number is the number that we need to multiply by itself to get the original number. If a is the square root of b, it means that a×a=b. For example, the square root of 9, √9 = 3. Such a type of question is generally asked in competitive exams and interviews.
Square root in JavaScript using Math.sqrt() function
In JavaScript, Math.sqrt() is a function that is used to return the square root of a number. Because the Math.sqrt() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
Syntax-Math.sqrt(x)
Here, x is the required number. If the provided number is negative, it returns NaN.
Example-
let a = Math.sqrt(9);
console.log(a);
let b = Math.sqrt(1);
console.log(b);
let c = Math.sqrt(16);
console.log(c);
let d = Math.sqrt(64);
console.log(d);
let e = Math.sqrt(-9);
console.log(e);
Output of the above code:
3
1
4
8
NaN
Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh