Home » JavaScript

Reverse an array in JavaScript

In this article, we will write a code to reverse an array in JavaScript and also see an in-built method.
Submitted by Abhishek Pathak, on October 16, 2017

Array reversal is an important array operation that a programmer should know and when you are working with JavaScript, it is even more important because most of the data in the webpage is in the form of objects and arrays.

Reversing an array is similar as traversing the array from the end. But, in this case, the element at last will be at first and so on. Looking deep in the problem, as a programmer, you'll see what we will be going to do. We will switch up the element by their index positions. The element at last is switched with the first element and second with second last and so on. The middle element, if there's any, doesn't require switch because it will be at the same position anyway.

So, let's write down the code,

var array = [1, 2, 3, 4, 5, 6];

//Storing the size of the array
var size = array.length;

for(var i=0; i<(size / 2); i++) {
	
	//Swapping first and last element
	var temp = array[i];
	array[i] = array[size-i-1];
	array[size-i-1] = temp;
}

//Final output
console.log(array);

In this code, we are first defining an array and then storing it's length in size variable using array.length property. Next, we run a for loop starting with 0 upto size/2 because once we reach half the array, the other half are already swapped elements.

Inside the for loop, we swap the element using the temp variable. First, we store the a[i], ie current element in loop, in the temp variable. Then we store the value of size-i-1 th element inside this current element and finally swap by storing the value of temp in the size-i-1 th element.

The size-i-1 can be written as, (size-1)-i, which means, the last element - i. As with every iteration beginning from 0, the i value increase, this means we are moving towards the center from the end, which is how we have to swap the elements.

Nice trick there! But, if you don't find this comfortable, JavaScript in-built function reverse() might save you. Just apply this property on any array and see how easily JavaScript does Array reversal for you.

Code

var array = [1, 2, 3, 4, 5];
array.reverse();
console.log(array);

Output

[5, 4, 3, 2, 1]

Hope you like this article. Share your thoughts in the comments below.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.