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}`); |
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