Find Perimeter of a Triangle in JavaScript



The perimeter of a triangle is the sum of the lengths of its three sides. You can find the perimeter of a triangle using JavaScript by finding the sum of all sides of the triangle, as the perimeter of the triangle refers to the sum of all three sides of the triangle, i.e., perimeter = a + b + c.

To find the perimeter of a triangle using JavaScript, we need to write a function that calculates the sum of three sides of a triangle and returns the triangle's perimeter as an output. In this article, we will understand the perimeter of a triangle formula, how to find it using JavaScript, and implement practical examples for this.

Understanding the Perimeter Formula

The formula for the perimeter of a triangle is straightforward. As we know, the perimeter of a triangle is the sum of the lengths of its sides, where a, b, and c are the lengths of the three sides of the triangle. Here is the formula for this:

Perimeter=a+b+c
perimeter

Suppose, we have

let a = 10, b=10, c= 10
// Then, the perimeter of a triangle will be
perimeter= a+b+c
perimeter= 30

Steps to Calculate Perimeter of Triangle

The following are the steps for finding perimeter of the triangle using JavaScript.

  • Firstly, have to stored sides of triangle as side1, side2 and side3.
  • Then, a function trianglePerimeter is defined that takes 3 argument i.e a, b and c which refers to sides of traingle, to calculate the perimeter of triangle.
  • The function returns perimeter by calculating sum of sides of triangle.
  • The perimeter of the triangle is calculated by calling the trianglePerimeter() function and passing side1, side2, and side3 as arguments.
  • The output is displayed in web console using console.log().

Example 1

This example demonstrates the implementation of the above steps to find the perimeter of the triangle using JavaScript.

let side1 = 10, side2 = 10, side3 = 10;
function trianglePerimeter(a, b, c) {
    return a + b + c;
}
let perimeter = trianglePerimeter(side1, side2, side3);
console.log("The perimeter of the triangle is: " +perimeter);

Output

The perimeter of the triangle is: 30

Example 2

In the above example, we have implemented the perimeter of a triangle by writing a simple JavaScript function. In this example, we are using the same function, but the values will be taken from the user.

let side1 = parseInt(prompt("Enter the first side of the triangle:"));
let side2 = parseInt(prompt("Enter the second side of the triangle:"));
let side3 = parseInt(prompt("Enter the third side of the triangle:"));

function trianglePerimeter(a, b, c) {
    return a + b + c;
}
let perimeter = trianglePerimeter(side1, side2, side3);
console.log("The perimeter of the triangle is: " +perimeter);

Conclusion

In this article, we have implemented the perimeter of the triangle using JavaScript. This is the simple approach for finding the triangle's perimeter. This example helps you understand the basics of using geometric formulas in JavaScript programming.

Updated on: 2025-03-17T12:32:54+05:30

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements