
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Area and Perimeter of Rectangle in JavaScript
To find the area and perimeter of a rectangle in JavaScript, we will be using the basic formulas for the perimeter and area of a rectangle. The area of a rectangle is the multiplication of its length by its breadth, and the perimeter of a rectangle is the sum of the lengths of all its four sides.
In this article, we are going to understand the formula for the area and perimeter of a rectangle, how to find them using JavaScript, and implement practical examples for this.
Understanding the Formulas
The formulas for the area and perimeter of a rectangle are simple and easy to use. Here are the formulas:
Area of a Rectangle: Area = length à breadth Perimeter of a Rectangle: Perimeter=2 à (length + breadth)

Suppose, we have
length=10, breadth= 13 // Then, the perimeter of a rectangle will be perimeter= 2*(l+b) perimeter= 2*(10+13) perimeter= 46 // And, the area of a rectangle will be Area= length*breadth Area= 10*13 Area= 130
Steps to Find Area and Perimeter of Rectangle
The following are the steps for finding area and perimeter of a rectangle using JavaScript.
- We have initialized two variables: width and height of the rectangle. The initial value is set to length= 10 and breadth= 1.3
- First, two functions are defined perimeter() and area() to calculate the perimeter and area of the rectangle, respectively.
- The area function takes two arguments l and b and returns the result of l * b, where l and b are length and breadth respectively.
- The perimeter function takes two arguments l and b and returns the result of 2*(l + b).
- The area and perimeter of the rectangle are calculated by calling the functions area() and perimeter() by passing length and breadth variables as arguments, and is stored in the variables area_rect and perimeter_rect.
- The output is displayed in web console using console.log().
Example 1
This example demonstrates the implementation of the above steps to find the area and perimeter of a rectangle using JavaScript.
let length=10, breadth=13; function perimeter(l, b) { return 2*(l+b); } function area(l, b){ return l*b; } let perimeter_rect = perimeter(length, breadth); let area_rect = area(length, breadth); console.log("Perimeter of rectangle: " +perimeter_rect); console.log("Area of rectangle: " +area_rect);
Output
Perimeter of rectangle: 46 Area of rectangle: 130
Example 2
In the above example, we have implemented the area and perimeter of a rectangle 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 length= parseInt(prompt("Enter the length of a rectangle:")); let breadth= parseInt(prompt("Enter the breadth of a rectangle:")); function perimeter(l, b) { return 2*(l+b); } function area(l, b){ return l*b; } let perimeter_rect = perimeter(length, breadth); let area_rect = area(length, breadth); console.log("Perimeter of rectangle: " +perimeter_rect); console.log("Area of rectangle: " +area_rect);
Conclusion
In this article, we have implemented the area and perimeter of a rectangle using JavaScript. This JavaScript program makes it easy to calculate the area and perimeter of a rectangle. This simple concept helps create applications that deal with geometry and mathematical calculations.