Open In App

JavaScript Program to Find Perimeter of Square

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given the length of one side, our task is to find the perimeter of a square in JavaScript. The perimeter of a square is the total length of its four sides. It can be calculated by multiplying the length of one side by 4 and all its angles measure 90 degrees.

Example:

Formula for calculating Perimeter of Square is: 4*s 
Where, s is sides of a square

Input:
 4 
Output:
16

Use the below approaches to find the Perimeter of the Square:

Table of Content

Using Function

In this approach, We will create a function that takes one parameter, the side of the square. Inside the function, calculate the perimeter of the square by multiplying the side length by 4 (as all sides of the square are equal). Return the calculated perimeter value.

Example: The example below shows the demonstration of finding the perimeter of the square using the function

JavaScript
function findPerimeter(side) {
    return 4 * side;
}
const sideLength = 4;

// Call the function 
const perimeter = findPerimeter(sideLength);
console.log("Perimeter of square is :",
                            perimeter);

Output
Perimeter of square is : 16

Time complexity: O(1)

Space complexity: O(1)

Using Class

In this approach, define a class named Square. Within the class, a constructor method is defined to initialize the side length of the square. Additionally, a method is defined to calculate the perimeter of the square using the formula 4 * side. To utilize this class, an instance of Square is created by passing the side length as an argument to the constructor. Finally, the method for calculating the perimeter is called on the instance, and the result is printed.

Example: The example below shows the demonstration of finding perimeter of square using class.

JavaScript
class Square {
    
    // Constructor method
    constructor(side) {
        this.side = side;
    }
    getPerimeter() {
        return 4 * this.side;
    }
}

// Create an instance of Square
const square = new Square(4);
const perimeter = square.getPerimeter();
console.log("Perimeter of square is :", perimeter);

Output
Perimeter of square is : 16

Time Complexity: O(1)

Space Complexity: O(1)


Previous Article
Next Article

Similar Reads

JavaScript program to find Area and Perimeter of Rectangle
A rectangle is a flat figure in a plane. It has four sides and four equal angles of 90 degrees each. In a rectangle all four sides are not of equal length like a square, sides opposite to each other have equal length. Both diagonals of the rectangle have equal lengths. Examples: Input: 4 5 Output: Area = 20 Perimeter = 18 Input: 2 3 Output: Area =
1 min read
JavaScript program to find perimeter of a triangle
Given the side (a, b, c) of a triangle and we have to find the perimeter of a triangle. Perimeter: Perimeter of a triangle is the sum of the length of the side of a triangle. Where a, b, and c are lengths of sides of a triangle. The perimeter of a triangle can be simply evaluated using the following formula: Perimeter = (a + b + c) Examples: Input
1 min read
JavaScript Program to Find Perimeter of Parallelogram
Given the length of one side, our task is to find the perimeter of a parallelogram in JavaScript. A parallelogram is a geometric shape having four sides and its opposite sides are parallel and equal in length, and its opposite angles are also equal. The perimeter of a parallelogram is the total length of its four sides. It can be calculated by addi
2 min read
PHP Program to Find Area and Perimeter of Rectangle
Calculating the area and perimeter of a rectangle is a fundamental geometric operation. The area of a rectangle is given by the formula Area = length * width, and the perimeter is given by Perimeter = 2 * (length + width). In this article, we will explore how to calculate the area and perimeter of a rectangle in PHP using different approaches. Tabl
2 min read
PHP Program to Find Area and Perimeter of Circle
Given a Radius of Circle, the task is to find the Area and Perimeter of Circle in PHP. To find the area, use the formula π * r2, where r is the radius of the circle. To find the perimeter (circumference), use the formula 2 * π * r. There are two approaches to calculate the area and perimeter (circumference) of a circle using PHP: Table of Content U
2 min read
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 SquareArea= side * sideBelow are the methods by which we
3 min read
JavaScript Program to Find the Square Root
Given a number N, the task is to calculate the square root of a given number using JavaScript. There are the approaches to calculate the square root of the given number, these are: Approaches to Find the Square Root of Given Number in JavaScript: Table of Content Using Math.sqrt() MethodUsing JavaScript Math.pow() MethodUsing Binary SearchNewton's
5 min read
Javascript Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input: N = 7Output:1 2 3 4 5 6 72 3 4 5 6 7 13 4 5 6 7 1
3 min read
Javascript Program for Maximum and Minimum in a square matrix.
Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix separately using linear search. Number of comparis
3 min read
JavaScript Program for Chi-Square Critical Value Calculator
In this article, we will discuss the Chi-Square Critical Value Calculator. The Chi-Square Critical Value Calculator is a JavaScript tool used to determine critical values from the chi-square distribution. Chi-Square critical values help you decide if your study results are strong enough to conclude that there's a real difference or if it might just
2 min read