JavaScript Math SQRT2
Syntax & Examples


Math.SQRT2 static-property

The Math.SQRT2 property represents the square root of 2, which is approximately equal to 1.414.


Syntax of Math.SQRT2

The syntax of Math.SQRT2 static-property is:

Math.SQRT2

This SQRT2 static-property of Math square root of 2; approximately 1.414.



✐ Examples

1 Using Math.SQRT2 to calculate a diagonal of a square

In JavaScript, we can use the Math.SQRT2 property to calculate the diagonal of a square given its side length.

For example,

  1. We create a variable sideLength and set it to 5.
  2. We calculate the diagonal of the square using the formula sideLength * Math.SQRT2.
  3. The result is stored in the variable diagonal.
  4. We log diagonal to the console using the console.log() method.

JavaScript Program

const sideLength = 5;
const diagonal = sideLength * Math.SQRT2;
console.log(diagonal);

Output

7.0710678118654755

2 Using Math.SQRT2 in a geometric transformation

In JavaScript, we can use the Math.SQRT2 property in geometric transformations, such as scaling coordinates by the square root of 2.

For example,

  1. We create variables x and y representing coordinates.
  2. We calculate the scaled coordinates using x * Math.SQRT2 and y * Math.SQRT2.
  3. The results are stored in variables scaledX and scaledY.
  4. We log scaledX and scaledY to the console using the console.log() method.

JavaScript Program

const x = 3;
const y = 4;
const scaledX = x * Math.SQRT2;
const scaledY = y * Math.SQRT2;
console.log(scaledX, scaledY);

Output

4.242640687119285 5.656854249492381

3 Using Math.SQRT2 in a trigonometric function

In JavaScript, we can use the Math.SQRT2 property in trigonometric functions to simplify calculations involving right triangles.

For example,

  1. We create variables a and b representing the legs of a right triangle.
  2. We calculate the hypotenuse using the Pythagorean theorem, which involves Math.SQRT2.
  3. The result is stored in the variable hypotenuse.
  4. We log hypotenuse to the console using the console.log() method.

JavaScript Program

const a = 1;
const b = 1;
const hypotenuse = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
console.log(hypotenuse);

Output

1.4142135623730951

Summary

In this JavaScript tutorial, we learned about SQRT2 static-property of Math: the syntax and few working examples with output and detailed explanation for each example.