JavaScript Program to Calculate the Area of a Triangle

What is the Area of a Triangle?

Area is defined as the region of a shape or object that is occupied by its boundary or surface. In a two-dimensional plane, this means the area of a triangle is the region enclosed by it, in a two-dimensional plane.

As we know, a triangle is a closed shape that has three sides and three vertices. Thus, the area of a triangle is the total space occupied within the three sides of a triangle. The general formula to find the area of the triangle is given by half of the product of its base and height.

JavaScript Program to Calculate the Area of a Triangle

The measurement of area is done in square units with the standard unit being square meters (m2). For the computation of area, there are predefined formulas for squares, rectangles, circles, triangles, etc. In this article, we will learn how to find areas of different types of triangles with some example problems!

Prerequisites

Contents

  1. Calculate the Area of a Right-Angled Triangle
  2. Calculate the Area of a Scalene Triangle
  3. Calculate the Area of an Equilateral Triangle
  4. Calculate the Area of an Isosceles Triangle

1. Calculate the Area of a Right-Angled Triangle

What is a Right-Angled Triangle?

A right-angled triangle, also called a right triangle, has one angle equal to 90° and the other two acute angles sum up to 90°. Therefore, the height of the triangle is the length of the perpendicular side.

When the base and height of a triangle are given, the area of the right-angled triangle can be calculated using the following formula:

Area of an Right-Angled Triangle

Here's an example to calculate the area of a right-angled triangle in JavaScript:

// prompt the user to enter the base and height of the triangle
const base = prompt('Enter the base of the triangle: ');
const height = prompt('Enter the height of the triangle: ');

// calculate the area of the triangle
const area = (base * height) / 2;

// display the area of the triangle
console.log(`The area of the triangle is ${area} square units.`);

When you run the above program, the user will be prompted to enter the base and height of the triangle and the program will then calculate the area of the triangle and display it.

Enter the base of a triangle: 10
Enter the height of a triangle: 20
The area of the triangle is 100 square units.

2. Calculate the Area of a Scalene Triangle

What is a Scalene Triangle?

A scalene triangle is a triangle that has three sides of different lengths.

When all the sides of a triangle are known, the area of the scalene triangle can be calculated using the following formula, which is also known as Heron's Formula:

Area of a Scalene Triangle Using Heron's Formula

where, a, b, and c are the lengths of the three sides of the triangle, and s is the semiperimeter of the triangle which is sum of all the sides divided by two.

Semi Perimeter of a Triangle

Here's an example to calculate the area of a scalene triangle in JavaScript using the above formula.

// declare the variables to store the lengths of the sides of the triangle
const side1 = 10;
const side2 = 10;
const side3 = 10;

// calculate the semi perimeter of the triangle
const semiPerimeter = (side1 + side2 + side3) / 2;
console.log(semiPerimeter);

// calculate the area of triangle using heron's formula
// the sides should make a valid triangle for this to work otherwise the formula will return 0
const area = Math.sqrt(
  semiPerimeter *
    (semiPerimeter - side1) *
    (semiPerimeter - side2) *
    (semiPerimeter - side3)
);

// display the area of the triangle
console.log(`The area of the triangle is ${area} square units.`);

When you run the above program, the program will calculate the semi perimeter of the triangle first and then calculate the area of the triangle and display it.

3. Calculate the Area of an Equilateral Triangle

What is an Equilateral Triangle?

An equilateral triangle is a triangle where all the sides are equal. The perpendicular drawn from the vertex of the triangle to the base divides the base into two equal parts. To calculate the area of the equilateral triangle, we need to know the length of one side because all sides are equal.

Area of an Equilateral Triangle

Here's an example to calculate the area of an equilateral triangle in JavaScript using the above formula.

// prompt the user to enter the length of the sides of the triangle
const side = prompt('Enter the length of the sides of the triangle: ');

// calculate the area of the triangle
const area = (Math.sqrt(3) / 4) * (side * side);

// display the area of the triangle
console.log(`The area of the triangle is ${area} square units.`);

4. Calculate the Area of an Isosceles Triangle

What is an Isosceles Triangle?

An isosceles triangle is a triangle that has two sides of equal length.

Here's the formula to calculate the area of an isosceles triangle, where b is the base and a is the length of an equal side.

Area of an Isosceles Triangle

Here's an example to calculate the area of an isosceles triangle in JavaScript using the above formula.

// prompt the user to enter the length of the base of the triangle
const base = prompt('Enter the length of the base of the triangle: ');

// prompt the user to enter the length of the side of the triangle
const side = prompt('Enter the length of the side of the triangle: ');

// calculate the area of the triangle
const area = (1 / 4) * base * Math.sqrt(4 * side * side - base * base);

// display the area of the triangle
console.log(`The area of the triangle is ${area} square units.`);

Summary

In this tutorial, we have learnt how to calculate the area of a right-angled triangle, a scalene triangle, an equilateral triangle, and an isosceles triangle. We have also learnt how to calculate the area of a triangle using the Heron's formula.

Please checkout our related content section below for more tutorials on similar topics.


We hope that you have learnt something from our tutorials. Please feel free to share this content with your friends.


If you have any questions or want some other content to be added to our website so that it can help you learn and grow further, please do not hesitate to contact us at info@createmysite.org. We would love to hear from you.

We are hiring!! If you are tech blogger who loves writing content on modern technologies, we would love to get in touch with you. Just drop us a mail at the email address mentioned above and we will reach out to you as soon as possible.

If you are interested in learning more about our website, please visit our about page.