How to Create a Simple JavaScript Function to Calculate the Area of a Rectangle?

Estimated read time 1 min read

Here is an example to create a simple JavaScript function to calculate the area of a rectangle:

function calculateArea(width, height) {
  return width * height;

var rectangleWidth = 5;
var rectangleHeight = 10;
var rectangleArea = calculateArea(rectangleWidth, rectangleHeight);

console.log("The area of a rectangle with width " + rectangleWidth + " and height " + rectangleHeight + " is " + rectangleArea);

This code defines a function calculateArea that takes two parameters, width and height, and returns their product. The last three lines of the code create variables to store the rectangle’s dimensions, call the calculateArea function, and log the result to the console.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply