
- 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
How to get the square root of 2 in JavaScript?
In this tutorial, we shall learn to get the square root of 2 in JavaScript.
The square root of 2 is an irrational number, and it is called Pythagoras's constant. We can’t denote it in fractions, and it has an infinite number of decimals; hence, we can’t find its exact value. The √2 value is 1.414.
HTML encoding of the square root symbol in JavaScript is √.
Next, we will discuss the available method to find the square root of 2.
Using Math object's SQRT2 property
In this part, we will see how the SQRT2 property of the Math object works. It is an ES1 feature, and this property is not writable, not enumerable, and not configurable. We always need to use it as Math.SQRT2 because this is another static property of the Math object.
This is a constant used in mathematics equations. We don’t need to calculate it every time because it is a constant, and it is very precise and used widely
It is generally used to solve problems with circular figures. The return type of this property is a number, a property in JavaScript version 1.0.
Syntax
We can follow the syntax below to use this property.
Math.SQRT2
In the syntax above, Math.SQRT2 returns the square root of 2. There are no parameters because this is not a function.
Example
In this example program, we get the square root of 2 using Math.SQRT2. This value is displayed as the output.
<html> <head> <title>JavaScript Math SQRT2 Property</title> </head> <body> <div id="result"></div> <script> var sqrtVal = Math.SQRT2; document.getElementById("result").innerHTML = "The square root of 2 = " + sqrtVal; </script> </body> </html>
Example
In this example program, we make a call to a function with a number as an argument. This function returns the outcome of Math.SQRT2 with the argument value.
We use a try-catch block here to handle the exception here. The outcome from the catch block is displayed as the output, an error output because of Math.SQRT2. This is because it is a property, not a function, that can take an argument.
<html> <body> <p id="sqrtFnId"></p> <script> function getSqrtFn(val) { try { return Math.SQRT2(val); } catch (e) { return e; } } var sqrtFnVal = getSqrtFn(10); var sqrtFnEl = document.getElementById("sqrtFnId"); sqrtFnEl.innerHTML = "Math.SQRT2(10) " + sqrtFnVal; </script> </body> </html>
Using the Math.sqrt() Function
We can use the Math.sqrt() function to find the square root of the 2. It returns the square root of the given number. It takes a number as a parameter. If the number is negative, it returns NaN. The return type of this property is a number.
Syntax
We can follow the syntax below to use Math.sqrt() function.
Math.sqrt(2)
In the syntax above, Math.sqrt() returns the square root of 2.
Example
In this example program, we get the square root of 2 using Math.sqrt() function. This value is displayed as the output.
<html> <body> <div id="result"></div> <script> var sqrtVal = Math.sqrt(2); document.getElementById("result").innerHTML = "The square root of 2 = " + sqrtVal; </script> </body> </html>
This tutorial helped us to learn about the SQRT2 property of the Math object in JavaScript.
It can be used in any mathematical calculation as a constant, and the property returns the square root of 2.
We can’t use Math.SQRT2 as a function by giving parameters, which will throw an exception in this case.
The second method, we discussed to find the square root of 2 is the Math.sqrt().
- Related Articles
- How to get the square root of a number in JavaScript?
- Using square root table, find the square root of the following (up to 2 decimal places)$ 83.17$
- How to find the Square root of 169?
- PHP – How to get the square root of an arbitrary precision number using bcsqrt() function?
- 8086 program to find the square root of a perfect square root number
- Get square root of a number using Math.sqrt in Java
- How to find perfect square root?
- Square root function without using Math.sqrt() in JavaScript
- How to calculate square root of a number in Python?
- How to find Square root of complex numbers in Python?
- How to find the root mean square of a vector in R?
- How to solve square root in division method?
- Finding square root of a number without using Math.sqrt() in JavaScript
- Find the square root of 2025.
- Find the square root of 3.5.
