Area and Perimeter of Rectangle
Program to calculate area and perimeter of a rectangle
JavaScript Area and Perimeter of Rectangle Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Formula: Area = length × width
// Formula: Perimeter = 2 × (length + width)
let length = 10;
let width = 5;
let area = length * width;
let perimeter = 2 * (length + width);
console.log("Rectangle Dimensions:");
console.log("Length:", length);
console.log("Width:", width);
console.log("Area:", area, "square units");
console.log("Perimeter:", perimeter, "units");
// Function version
function rectangleArea(length, width) {
return length * width;
}
function rectanglePerimeter(length, width) {
return 2 * (length + width);
}
let rectLength = 8;
let rectWidth = 6;
console.log("\nUsing functions:");
console.log(`Area: ${rectangleArea(rectLength, rectWidth)} sq units`);
console.log(`Perimeter: ${rectanglePerimeter(rectLength, rectWidth)} units`);Rectangle Dimensions: Length: 10 Width: 5 Area: 50 square units Perimeter: 30 units Using functions: Area: 48 sq units Perimeter: 28 units
Understanding Area and Perimeter of Rectangle
This program calculates the area and perimeter of a rectangle using geometric formulas.
Rectangle Formulas
1.
Area
: length × width
2.
Perimeter
: 2 × (length + width)
Example Calculation
For a rectangle with length = 10 and width = 5:
Function Approach
Creating separate functions for each calculation:
function rectangleArea(length, width) {
}
function rectanglePerimeter(length, width) {
return 2 * (length + width);
}
return length * width;Benefits:
Related Shapes
Square
(special rectangle where length = width):
function squareArea(side) {
}
function squarePerimeter(side) {
return 4 * side;
}
return side * side; // or side ** 2Real-world Applications
Input Validation
Always validate inputs:
function rectangleArea(length, width) {
if (length <= 0 || width <= 0) {
}
return length * width;
}
return "Invalid dimensions";Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Area and Perimeter of Rectangle
This JavaScript program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.