
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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().