How to Find the Hypotenuse of a Right Triangle in JavaScript

08/01/2021

Contents

In this article, you will learn how to find the hypotenuse of a right triangle in JavaScript.

Finding the hypotenuse of a right triangle in JavaScript

A right triangle is a triangle in which one angle measures 90 degrees. The side opposite to the right angle is called the hypotenuse. To find the hypotenuse of a right triangle in JavaScript, we can use the Pythagorean theorem, which states that:

Hypotenuse^2 = Base^2 + Height^2

where Base and Height are the lengths of the other two sides of the right triangle.

Examples

Finding the hypotenuse of a right triangle with a base of length 3 units and a height of length 4 units

const base = 3; // in units
const height = 4; // in units
const hypotenuse = Math.sqrt(base ** 2 + height ** 2);
console.log(hypotenuse); // 5

In this example, the length of the base is 3 units, the length of the height is 4 units, and the output is the length of the hypotenuse of the right triangle, which is 5 units.

Finding the hypotenuse of a right triangle with a base of length 5 units and a height of length 12 units

const base = 5; // in units
const height = 12; // in units
const hypotenuse = Math.sqrt(base ** 2 + height ** 2);
console.log(hypotenuse); // 13

In this example, the length of the base is 5 units, the length of the height is 12 units, and the output is the length of the hypotenuse of the right triangle, which is 13 units.

Finding the hypotenuse of a right triangle with a base of length 8 units and a height of length 15 units

const base = 8; // in units
const height = 15; // in units
const hypotenuse = Math.sqrt(base ** 2 + height ** 2);
console.log(hypotenuse); // 17

In this example, the length of the base is 8 units, the length of the height is 15 units, and the output is the length of the hypotenuse of the right triangle, which is 17 units.