JavaScript Square

In this article, we will explore the concept of a square in JavaScript.

Squaring a number

When a number is multiplied by itself, the process is referred to as squaring that number.

For instance, let us take the variable "a". We can perform a multiplication of "a" by itself in the following manner ( a * a ).

In the field of mathematics, it is denoted in the following manner:

a^2 (It is called a is to the power 2.)

Calculating the square of a number has numerous practical uses, including determining distances, calculating areas, and more.

In JavaScript, we can multiply a number by itself by utilizing various methods, which are given below:

  • Utilizing the multiplication operator
  • Utilizing a function
  • Utilizing the Math.pow method
  • Utilizing the exponentiation operator
  • Utilizing the multiplication operator

The simplest method for calculating the square of a number is to make use of the multiplication operator.

Let us explore the application of the multiplication operator through a practical demonstration.

Code:

Example

const num = 6;
const num_square = num * num;
console.log(num_square);

Output:

In this output, we can observe the square of the number "6".

Utilizing a function

Another method to obtain the square of a number is by creating a function.

In the illustration that follows, we will develop a function that accepts a numerical input. This function is designed to return the square of the provided number.

Code:

Example

function numSquare(num) {
  return num * num;
}

const num = 10;
const outcome = numSquare(num);
console.log(outcome);

Output:

In the following output, we can distinctly observe the square of the number "10" being calculated through the creation of a function.

Utilizing the Math.pow method

An alternative method for squaring a number is to employ the Math.pow function.

The built-in Math object in JavaScript serves the purpose of performing a wide range of mathematical operations.

One such method that aids in calculating the power of a number is Math.pow.

Syntax:

Example

Math.pow(number, exponent)

This approach involves a pair of parameters, detailed below:

number: It is the base number.

exponent: This refers to the power associated with the base number, which in this case will be 2, as we are aiming to calculate the square.

Code:

Example

const num = 9;
const numberSquare = Math.pow(num, 2);
console.log(numberSquare);

Output:

We can observe the square of the number "9" achieved using the Math.pow function.

Utilizing the exponentiation operator

The exponentiation operator (**) serves as an alternative approach to repeatedly multiplying a number by itself.

Syntax:

Example

base ** exponent</p>
<p>The base is the number of which you want to find the square. The exponent is the power which should be 2 because we want to find out the square.

Code:

Example

const num =5;
const num_square = num ** 2;
console.log(num_square);

Output:

The output displayed below clearly illustrates the result of multiplying the number "5" by itself.

Squaring an array

The process of squaring an array refers to the operation where each element contained within the array is raised to the power of two.

In JavaScript, there are several approaches to squaring the elements of an array, which are outlined as follows:

  • Employing the map function
  • Utilizing the for loop
  • Utilizing the map function

The map function is employed in conjunction with the exponentiation operator (**) to compute the square of each element within an array.

We will explore the application of the map function to compute the square of an array through the following example.

Code:

Example

function squareArray(arr) { 
  return arr.map(function(num){
      return num ** 2
  })
}

console.log(squareArray([5, 3, 2, 9, 6]))

Output:

In the output presented below, we can observe the squaring of an array accomplished through the use of the map function.

Utilizing the for loop

We can utilize the "for" loop to square an array.

We will explore the application of the "for" loop to calculate the square of each element in an array through the following demonstration.

Code:

Example

let arr = [10, 8, 6, 5]
for (i = 0; i < 4; i++)
{
  arr[i] = arr [i] * arr[i]
}
console.log("Square of elements present in the array = ", arr)

Output:

In the following output presented below, we can observe the squaring of an array accomplished through the use of a for loop.

Conclusion:

  • We have understood the JavaScript square in this article.
  • We have understood that we can square a number and square an array in JavaScript.
  • There are various ways with the help of which we can square a number such as utilizing the multiplication operator, a function, the math.pow method and the exponentiation operator.
  • There are various ways with the help of which we can square an array such as utilizing the map function and the for loop.

Input Required

This code uses input(). Please provide values below: