JavaScript Program to Find Area and Perimeter of Rectangle

To find the area and perimeter of a rectangle in JavaScript, we use the basic formulas for rectangle calculations. The area of a rectangle is the multiplication of its length by its breadth, and the perimeter is the sum of all four sides.

In this article, we'll understand the formulas and implement practical JavaScript examples to calculate rectangle area and perimeter.

Understanding the Formulas

The formulas for rectangle calculations are straightforward:

Area = length × breadth
Perimeter = 2 × (length + breadth)
Length Breadth Area = Length × Breadth Perimeter = 2 × (Length + Breadth)

For example, with length = 10 and breadth = 13:

let length = 10, breadth = 13;

// Calculate perimeter
let perimeter = 2 * (length + breadth);
console.log("Perimeter calculation: 2 × (10 + 13) =", perimeter);

// Calculate area  
let area = length * breadth;
console.log("Area calculation: 10 × 13 =", area);
Perimeter calculation: 2 × (10 + 13) = 46
Area calculation: 10 × 13 = 130

Using Functions for Rectangle Calculations

Let's create reusable functions to calculate area and perimeter:

let length = 10, breadth = 13;

function calculatePerimeter(l, b) {
    return 2 * (l + b);
}

function calculateArea(l, b) {
    return l * b;
}

let rectanglePerimeter = calculatePerimeter(length, breadth);
let rectangleArea = calculateArea(length, breadth);

console.log("Rectangle dimensions: " + length + " × " + breadth);
console.log("Perimeter of rectangle: " + rectanglePerimeter);
console.log("Area of rectangle: " + rectangleArea);
Rectangle dimensions: 10 × 13
Perimeter of rectangle: 46
Area of rectangle: 130

Rectangle Calculator with Multiple Examples

Here's a more comprehensive example that calculates for different rectangles:

function rectangleCalculator(length, breadth) {
    const area = length * breadth;
    const perimeter = 2 * (length + breadth);
    
    return {
        length: length,
        breadth: breadth,
        area: area,
        perimeter: perimeter
    };
}

// Test with different rectangles
const rectangles = [
    rectangleCalculator(5, 8),
    rectangleCalculator(12, 7),
    rectangleCalculator(15, 10)
];

rectangles.forEach((rect, index) => {
    console.log(`Rectangle ${index + 1}:`);
    console.log(`  Dimensions: ${rect.length} × ${rect.breadth}`);
    console.log(`  Area: ${rect.area}`);
    console.log(`  Perimeter: ${rect.perimeter}`);
    console.log("");
});
Rectangle 1:
  Dimensions: 5 × 8
  Area: 40
  Perimeter: 26

Rectangle 2:
  Dimensions: 12 × 7
  Area: 84
  Perimeter: 38

Rectangle 3:
  Dimensions: 15 × 10
  Area: 150
  Perimeter: 50

Comparison of Different Rectangle Sizes

Length Breadth Area Perimeter
5 8 40 26
10 13 130 46
15 10 150 50

Conclusion

JavaScript makes it simple to calculate rectangle area and perimeter using basic mathematical formulas. These functions can be easily integrated into geometry applications or mathematical calculation tools.

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

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements