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
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.
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
Using an Arrow 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..
Syntax:
// Single parameter
const functionName = (parameter) => {
// Function body
};
// Multiple parameters
const functionName = (param1, param2) => {
// Function body
};
// No parameters
const functionName = () => {
// Function body
};
Example: In below example we created arrow function which takes side as parameter and return the area of square.
const squareArea = side => side * side; const sideLength = 7; const area = squareArea(sideLength); console.log(`The area of the square is: ${area}`); |
The area of the square is: 49
Using Math.pow() function
In this approach we will use Math.pow() function in JavaScript which is used to calculate the power of a number. In this case, Math.pow(sideLength, 2) calculates sideLength raised to the power of 2, which is equivalent to sideLength * sideLength, the area of the square.
Syntax:
Math.pow(base, exponent)
Example: The example uses Math.pow() function to calculate area of square. Here it calculate area by raising side length 8 to the power of 2 i.e. 64.
const sideLength = 8; const area = Math.pow(sideLength, 2); console.log(`The area of the square is: ${area}`); |
The area of the square is: 64
Using Object Literal Property Value Shorthand
This approach uses object literal property value shorthand to create an object with a sideLength property and an area property that calculates the area of the square.
Syntax:
const variableName = 'value';
const obj = { variableName };
VariableName: is the name of the variable that will be used as both the key and the value in the object.obj is the object being created.
Example: In below example object has a property sideLength with the value 10 and a property area that calculates the area by multiplying the side length by itself.
const sideLength = 10; const square = { sideLength,
area: sideLength * sideLength
}; console.log(`The area of the square is: ${square.area}`); |
The area of the square is: 100