Open In App

JavaScript program to find area of parallelogram

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A parallelogram is a geometric shape having four sides and its opposite sides are parallel and equal in length, and its opposite angles are also equal. We will use the formula area = base × height to return the area of a parallelogram in JavaScript.

Formula Used

// Formula for Area of Parallelogram 
area = base × height

Below are the approaches to Find the area of a Parallelogram:

Table of Content

Using Function

In this approach, Create a function that takes two input parameters one is the base of the parallelogram and the other is the height of the parallelogram. Inside the function, we will calculate the area using the formula for the area of a parallelogram: base × height. Call the function and return the calculated area.

Example: The below example shows how to demonstrate finding the area of a parallelogram using the function.

JavaScript
// Using function
function findArea(base, height) {
    return base * height;
}

const baseLength = 5;
const heightLength = 3;
const area = findArea(baseLength, heightLength);
console.log("Area of parallelogram is:", area);

Output
Area of parallelogram is: 15

Time Complexity: O(1)

Space Complexity: O(1)

Using Class

We will define a class named Parallelogram with a constructor method to initialize the base and height of the parallelogram, and we will define a method to calculate the area using the formula: base × height. Then we create an instance of the Parallelogram class and pass the base and height as arguments to the constructor. Call the method and print the result.

Example: The below example shows how to demonstrate finding the area of a parallelogram using class.

JavaScript
// Using Class
class Parallelogram {
    constructor(base, height) {
        this.base = base;
        this.height = height;
    }

    getArea() {
        return this.base * this.height;
    }
}

const parallelogram = new Parallelogram(5, 3);
const area = parallelogram.getArea();
console.log("Area of parallelogram is:", area);

Output
Area of parallelogram is: 15

Time Complexity: O(1)

Space Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads