How to use custom function In Javascript

In this approach, we will define a function that takes the side length as an argument and returns the square of the side length, representing the area of the square.

Syntax:

function functionName(parameter1, parameter2) {
    // Function body
}

Example: To demonstrate defining the areaOfSquare function which takes side as parameter which is 5 here and returns the area of square i.e. 25.

Javascript




function areaOfSquare(side) {
    return side * side;
}
 
const sideLength = 5;
const area = areaOfSquare(sideLength);
console.log(`The area of the square is: ${area}`);


Output

The area of the square is: 25

JavaScript Program to Find the Area of a Square

In this article, we will see how to find the area of a square in JavaScript if the side of the square is given. A square is a geometric shape with four equal sides and four right angles. The area of a square is calculated by multiplying the length of one side by itself.

Formula of the Area of Square

Area= side * side

Below are the methods by which we can find the area of a square in JavaScript:

Table of Content

  • Using custom function
  • Using an Arrow Function
  • Using Math.pow() function
  • Using Object Literal Property Value Shorthand

Similar Reads

Using custom function

In this approach, we will define a function that takes the side length as an argument and returns the square of the side length, representing the area of the square....

Using an Arrow Function

...

Using Math.pow() function

Here, an arrow function is used to calculate the area of a square. Arrow functions are a concise way to write functions in JavaScript. The function takes a side length parameter and returns the area by multiplying the side length by itself.....

Using Object Literal Property Value Shorthand

...