
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript program to find Area and Perimeter of Rectangle
We are writing a JavaScript program to calculate the area and perimeter of a rectangle. The program will prompt the user to input the width and length of the rectangle, and then we will use these values to calculate the area and perimeter. We will be continuously using these formulas: area = width * length, and perimeter = 2 * (width + length) to find the desired measurements.
Approach
The approach to find the Area and Perimeter of a rectangle in JavaScript can be done as follows −
Define the length and width of the rectangle using variables.
Calculate the Area by multiplying the length and width of the rectangle.
Calculate the Perimeter by adding the twice the length and twice the width of the rectangle.
Display the result of both Area and Perimeter.
Store the result in variables and return the values if needed.
Repeat the above steps for different sets of length and width as required.
Example
Here is an example of a JavaScript program that calculates the area and perimeter of a rectangle −
// Function to find the area of a rectangle function findArea(width, height) { return width * height; } // Function to find the perimeter of a rectangle function findPerimeter(width, height) { return 2 * (width + height); } // Input for width and height var width = 5; var height = 10; // Calculate the area and perimeter var area = findArea(width, height); var perimeter = findPerimeter(width, height); // Output the results console.log("Area of rectangle: " + area); console.log("Perimeter of rectangle: " + perimeter);
Explanation
First, two functions are defined - findArea and findPerimeter to calculate the area and perimeter of the rectangle, respectively.
The findArea function takes two arguments - width and height and returns the result of width * height.
The findPerimeter function takes two arguments - width and height and returns the result of 2 * (width + height).
The width and height of the rectangle are defined with var width = 5 and var height = 10.
The area and perimeter of the rectangle are calculated using the findArea and findPerimeter functions and stored in the variables area and perimeter.
Finally, the results are output to the console using console.log and displayed in the format "Area of rectangle: X" and "Perimeter of rectangle: Y", where X is the area and Y is the perimeter.
- Related Articles
- C Program for Area And Perimeter Of Rectangle
- Java Program to Find out the Area and Perimeter of Rectangle using Class Concept
- Program to find Perimeter / Circumference of Square and Rectangle in C++
- Java Program to Find the Perimeter of a Rectangle
- Swift Program to Find the Perimeter of a Rectangle
- Kotlin Program to Find the Perimeter of a Rectangle
- C++ Program to get minimum perimeter of rectangle whose area is n
- Java program to find the area of a rectangle
- Swift Program to find the area of the rectangle
- Golang program to find the area of a rectangle
- Haskell Program to Find the Area of a Rectangle
- The area of rectangle is 120 metre square and its breadth is 8 metre. Find the length of the rectangle and perimeter of the rectangle.
- Program to calculate area and perimeter of Trapezium
- Find the area and perimeter of the rectangle, if length $=46 cm$ and breadth $=25 cm$
- Program to find the Area and Perimeter of a Semicircle in C++
