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

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:-

- Using
**Base**and**Height**of the Triangle - 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:**