JavaScript Program To Calculate The Area of a Triangle

In this post, we will learn how to calculate the area of a triangle using JavaScript.

JavaScript Program To Calculate The Area of a Triangle

The area of a triangle is defined as the total space occupied by the three sides of a triangle in a 2-dimensional plane. 

The area of a triangle can be calculated using various methods. In this post, we will be calculating the area using two different approaches.

These two methods are as follows:-

  1. Using Base and Height of the Triangle
  2. Using the Sides of the Triangle

So, without further ado, let’s begin this tutorial.

JavaScript Program To Calculate The Area of a Triangle 

//JavaScript Program To Calculate The Area of a Triangle
var base = parseInt(prompt("Enter the base: "));
var height = parseInt(prompt("Enter the height: "));

//Calculating the area
var area = (base * height) / 2;

//Display Output
console.log("Base: " + base);
console.log("Height: " + height);
console.log("The area of the triangle is " + area);

Output

Base: 3
Height: 4
The area of the triangle is 6

How Does This Program Work ?

var base = parseInt(prompt("Enter the base: "));
var height = parseInt(prompt("Enter the height: "));

The user is asked to enter the value of base and height of the triangle.

//Calculating the area
var area = (base * height) / 2;

Then, we calculate the area of the triangle using ½ x Base x Height.

//Display Output
console.log("Base: " + base);
console.log("Height: " + height);
console.log("The area of the triangle is " + area);

Finally, the area of the triangle is printed on the screen using console.log() function.

JavaScript Program To Calculate Area of Triangle When All Sides are Known 

//JavaScript Program To Calculate The Area of a Triangle
var first_side = parseInt(prompt("Enter first side: "));
var second_side = parseInt(prompt("Enter second side: "));
var third_side = parseInt(prompt("Enter third side: "));

//Calculating semi-perimeter
var s = (first_side + second_side + third_side) / 2

//Calculating Area
var area = Math.sqrt(s * (s - first_side) * (s - second_side) * (s - third_side));
console.log("Area: " + area);

Output

Enter first side: 3
Enter second side: 4
Enter third side: 5
Area: 6
//Calculating semi-perimeter
var s = (first_side + second_side + third_side) / 2

We calculate the semi-perimeter of the triangle using s = (a + b + c) / 2.

var area = Math.sqrt(s * (s - first_side) * (s - second_side) * (s - third_side));

Then, we calculate the area of the triangle using the formula:

Area = [s((s-a)(s-b)(s-c))]**½ 

The Math.sqrt() method is used to calculate the square root of the number.

console.log("Area: " + area);

Finally, the area of the triangle is printed on the screen using the console.log() method.

Conclusion

I hope after going through this post, you understand how to calculate the area of a triangle using JavaScript.

If you have any query regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *