In this example, you will learn to write a javascript program to find the area of a triangle.

But before going into the example, you must have knowledge of the following JavaScript topics:

To find the area of a triangle we have two cases.

**Case 1**

If the base and height of a triangle are known to you, you can find the area using the following formula:

area = (base * height) / 2

**Example 1: **Area of Triangle When Base And Height are Known

```
const base = prompt('Enter the base of a triangle: ');
const height = prompt('Enter the height of a triangle: ');
//calculate the area
const area = (base * height) / 2;
console.log(`Area of the triangle is ${area}`);
```

**Output**

Enter the base of a triangle: 4

Enter the height of a triangle: 7

Area of the triangle is 14

**Case 2 **

If the length of the 3 sides is known to you, you can find the area using **Heron’s formula**.

s = (a+b+c)/2

area = √(s(s-a)(s-b)(s-c))

Here, `a`

, `b`

and `c`

are the 3 sides of the triangle

**Example 2:** Area of Triangle When All Sides are Known

```
const side1 = parseInt(prompt('Enter the length of side1: '));
const side2 = parseInt(prompt('Enter the length of side2: '));
const side3 = parseInt(prompt('Enter the length of side3: '));
//calculate the semi-perimeter
const s = (side1 + side2 + side3) / 2
//calculate the area
const area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
console.log(`Area of the triangle is ${area}`);
```

**Output**

Enter the length of side1: 3

Enter the length of side2: 4

Enter the length of side3: 5

Area of the triangle is 6

Here, we have used `Math.sqrt()`

to find the square root of the number.

**Example 3:** Area of an Equilateral Triangle

An equilateral triangle is a triangle whose sides are equal, you can find the area of an equilateral triangle using the formula :

area = (√3)/4 × side^{2}

```
const side = parseInt(prompt('Enter the length of the side: '));
//calculate the area
const area = (1.73 * side * side) / 4
console.log(`Area of the triangle is ${area}`);
```

**Output**

Enter the length of the side: 5

Area of the triangle is 10.8125