Area and Perimeter of Rectangle in JavaScript
Find the area and perimeter of a rectangle in JavaScript. Learn length × width and 2 × (length + width) using functions, with explanation and output.
1// Formula: Area = length × width2// Formula: Perimeter = 2 × (length + width)34let length = 10;5let width = 5;67let area = length * width;8let perimeter = 2 * (length + width);910console.log("Rectangle Dimensions:");11console.log("Length:", length);12console.log("Width:", width);13console.log("Area:", area, "square units");14console.log("Perimeter:", perimeter, "units");1516// Function version17function rectangleArea(length, width) {18 return length * width;19}2021function rectanglePerimeter(length, width) {22 return 2 * (length + width);23}2425let rectLength = 8;26let rectWidth = 6;2728console.log("\nUsing functions:");29console.log(`Area: ${rectangleArea(rectLength, rectWidth)} sq units`);30console.log(`Perimeter: ${rectanglePerimeter(rectLength, rectWidth)} units`);
Sample Output
Rectangle Dimensions: Length: 10 Width: 5 Area: 50 square units Perimeter: 30 units Using functions: Area: 48 sq units Perimeter: 28 units
In this one
- The formulas for rectangle area and perimeter
- How parentheses change the perimeter result
- How to split formulas into small functions
- Why input should be checked before math
How it works
- 1
length = 10 and width = 5 are set.
- 2
area = 10 * 5 = 50.
- 3
perimeter = 2 * (10 + 5) = 30.
- 4
The functions are called with 8 and 6.
- 5
They return 48 and 28, which are printed.
Edge cases
Negative sides
rectangleArea(-4, 5) returns -20.
The formula does not check input. Add a guard that rejects values less than or equal to 0.
Forgetting parentheses
2 * length + width gives 25, not 30.
Multiplication happens first. Write 2 * (length + width).
What's going on
This program finds two things about a rectangle. The area is the space inside it. The perimeter is the distance around its edges. Think of a classroom floor. The area tells you how many tiles you need. The perimeter tells you how much border tape you need.
Area is length × width. For a 10 by 5 rectangle the area is 50 square units. Perimeter is 2 × (length + width). Here it is 2 × 15 = 30 units. The parentheses make sure you add the sides before doubling.
The second half puts each formula in its own function: rectangleArea and rectanglePerimeter. Each one takes length and width and returns a number. With 8 and 6, the area is 48 and the perimeter is 28. Small functions like these are easy to test and reuse.
Be careful with names. Width is not a diagonal. The diagonal is the line from corner to corner, and it needs a different formula. Also, area and perimeter are easy to mix up, and the program will not warn you. Both give a number, so check your answer with a simple case like a 1 by 1 square.
The formulas also accept bad input. A negative length still gives a number, but it makes no sense. A real program should reject negative or zero sides before calculating.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Perimeter of a 4 by 3 rectangle?
Pick an answer to check it.
Question 2
Area of a 7 by 2 rectangle?
Pick an answer to check it.
Questions people ask
Why are the units different for area and perimeter?
Area measures surface, so it is in square units. Perimeter measures length, so it is in plain units.
How do I find the area of a square?
A square is a rectangle with equal sides. Call rectangleArea(side, side).
Should I use one function that returns both?
You can return an object like { area, perimeter }. Two small functions are also fine.