JavaScript

How to Reverse an Array in JavaScript Without Using Libraries

While processing the data in bulk, there can be a requirement to change the sorting. For instance, retrieving the values in descending instead of ascending manner. Also, to change the precedence of the included values. In such scenarios, reversing an array in JavaScript assists in manipulating and updating the data efficiently.

This blog will illustrate the methods to reverse an array without using libraries in JavaScript.

How to Reverse an Array Without Using Libraries in JavaScript?

To reverse an array in JavaScript without using libraries, apply the following approaches:

Method 1: Reverse an Array Using for Loop and push() Method in JavaScript

The “push()” method appends elements to the end of an array. This method, along with the “for” loop, can be utilized to iterate along the given array from the last and append the iterated elements into a new array.

Syntax

array.push(item1, item2, ..., itemX)

In the given syntax, “item1” and “item2” correspond to the items that need to be pushed into the associated array.

Example
Let’s go through the below-given example:

<script>
let myArray = [1, 2, 3, 4, 5, 6,]
console.log('Given Array: ', myArray);
let newArray = [];
for (let i = myArray.length-1; i >= 0; i--) {
newArray.push(myArray[i])
}
console.log('Reversed Array: ', newArray);
</script>

In the above lines of code:

  • Create an array named “myArray” and display it on the console.
  • In the next step, create a null array to store the pushed elements in a reversed manner.
  • In the next line, apply a “for” loop to iterate along the array starting from the end with the help of the “length” property.
  • Note that “-1” signifies that the length is synchronized accurately with the index.
  • Now, associate the “push()” method with the null array and append it with the iterated elements in the reverse sequence.
  • Lastly, display the reversed array.

Output

The output indicates that the given array is reversed.

Method 2: Reverse an Array Using reverse() Method in JavaScript

The “reverse()” method reverses the array element. This method can be applied to simply reverse the items in a provided array.

Example
Let’s overview the below-stated example:

<script>
let myArray = [1,2,3,4,5,6];
console.log('Given Array: ', myArray);
myArray.reverse();
console.log('Reversed Array: ', myArray);
</script>

In the above code snippet:

  • Likewise, create an array named “myArray” and display it on the console.
  • In the next step, associate the “reverse()” method with the declared array and display it on the console.
  • This will resultantly the reverse of the provided array.

Output

From the above outcome, it is evident that the given array is reversed successfully.

Conclusion

To reverse an array without using libraries in JavaScript, use the “for” loop and the “push()” method, in combination or the “reverse()” method. The former approaches can be utilized to append the iterated elements into a new array in reverse sequence. The latter approach simply reverses the element via its allocated method. This blog discussed the approaches to reverse an array without using libraries in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.