JavaScript Program to Find Perimeter of a Triangle

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 calculating the sum of all sides, where perimeter = a + b + c.

To find the perimeter of a triangle using JavaScript, we need to write a function that adds the three side lengths together. In this article, we will understand the perimeter formula and implement practical examples.

Understanding the Perimeter Formula

The formula for the perimeter of a triangle is straightforward. The perimeter equals the sum of all three sides:

Perimeter = a + b + c
a b c Triangle Perimeter = a + b + c

For example, if we have a triangle with sides 5, 7, and 8:

let a = 5, b = 7, c = 8;
let perimeter = a + b + c;
console.log("Perimeter:", perimeter);
Perimeter: 20

Basic Implementation

Here's a simple function to calculate triangle perimeter:

function trianglePerimeter(side1, side2, side3) {
    return side1 + side2 + side3;
}

// Example with different triangles
let triangle1 = trianglePerimeter(3, 4, 5);
let triangle2 = trianglePerimeter(10, 15, 20);

console.log("Triangle 1 perimeter:", triangle1);
console.log("Triangle 2 perimeter:", triangle2);
Triangle 1 perimeter: 12
Triangle 2 perimeter: 45

Enhanced Version with Validation

This example includes validation to check if the sides can form a valid triangle:

function calculateTrianglePerimeter(a, b, c) {
    // Check if sides form a valid triangle
    if (a + b > c && b + c > a && a + c > b) {
        return a + b + c;
    } else {
        return "Invalid triangle: sides don't satisfy triangle inequality";
    }
}

// Valid triangle
console.log("Valid triangle (3, 4, 5):", calculateTrianglePerimeter(3, 4, 5));

// Invalid triangle
console.log("Invalid triangle (1, 2, 10):", calculateTrianglePerimeter(1, 2, 10));
Valid triangle (3, 4, 5): 12
Invalid triangle (1, 2, 10): Invalid triangle: sides don't satisfy triangle inequality

Practical Example with Multiple Triangles

Here's how to calculate perimeters for multiple triangles using an array:

const triangles = [
    [6, 8, 10],    // Right triangle
    [5, 5, 5],     // Equilateral triangle
    [3, 4, 5],     // Another right triangle
    [7, 10, 12]    // Scalene triangle
];

function findPerimeters(triangleArray) {
    return triangleArray.map((triangle, index) => {
        const [a, b, c] = triangle;
        const perimeter = a + b + c;
        return `Triangle ${index + 1}: ${perimeter}`;
    });
}

const results = findPerimeters(triangles);
results.forEach(result => console.log(result));
Triangle 1: 24
Triangle 2: 15
Triangle 3: 12
Triangle 4: 29

Key Points

  • Simple Formula: Perimeter = side1 + side2 + side3
  • Validation: Check triangle inequality (sum of any two sides > third side)
  • Data Types: Ensure sides are numeric values
  • Reusability: Create functions for repeated calculations

Conclusion

Calculating triangle perimeter in JavaScript is straightforward using the formula a + b + c. Adding validation ensures your program handles invalid triangles correctly, making it more robust for real-world applications.

Updated on: 2026-03-15T23:19:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements