{"id":677,"date":"2023-11-29T07:27:13","date_gmt":"2023-11-29T07:27:13","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=677"},"modified":"2023-12-04T08:42:10","modified_gmt":"2023-12-04T08:42:10","slug":"reverse-array-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/reverse-array-javascript\/","title":{"rendered":"Reverse an Array in JavaScript | reverse() method"},"content":{"rendered":"\n<p>Reversing an array is changing the order of array elements to its opposite, such that the last elements occupy the first positions now. It will be helpful in various problems. In this article, we will discuss the different methods to reverse an array in JavaScript. So, let&#8217;s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong>What does it mean to Reverse an Array?<\/strong><\/strong><\/h2>\n\n\n\n<p>As we know, arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. For example, Arr = [ 1, 2, 10, 20 ]\u00a0 is an array of integers.<\/p>\n\n\n\n<p>Reversing an array means changing the order of the array to its opposite. If we have an array as [1,2,3,4,5], the reverse of this array is [5,4,3,2,1].&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-1024x320.png\" alt=\"example of reversed array\" class=\"wp-image-678\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-1024x320.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-300x94.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-768x240.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-750x234.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev-1140x356.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/rev.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>We can perform the reversal in primarily two ways.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We can either perform the in-place reversal. The original array\u2019s elements are reversed inside the array.\u00a0<\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>We can make a new array and push the elements of the original array in reverse order. The new array is called the Reversed Array.\u00a0<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4 Methods to Reverse an Array in JavaScript<\/strong><\/h2>\n\n\n\n<p>In this section, we will look at various methods for reversing an array:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the reverse() method<\/strong><\/h3>\n\n\n\n<p><strong>The simplest way to reverse an array in JavaScript is by using the built-in reverse() method. This method reverses the order of the elements within the array. It means that this is an in-place reversal of the array.<\/strong><\/p>\n\n\n\n<p>Here\u2019s an example of how to use reverse() method in JavaScript:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[4,5,6,7];\n\n\/\/printing the array\nconsole.log(ogArray);\n\n\/\/using the reverse function\nogArray.reverse();\n\n\/\/printing the reversed ogArray\nconsole.log(ogArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 4, 5, 6, 7 ]\n&#91; 7, 6, 5, 4 ]\n<\/code><\/pre>\n\n\n\n<p>So, in this example, we have used the in-built reverse function on the original array. We observe that the original array has been reversed, and we have obtained the desired output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the for() loop<\/strong><\/h3>\n\n\n\n<p>The second method is to make a new array to store the array elements in the reverse order. We have to iterate over the original array in the opposite direction using the for() loop.\u00a0<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[4,5,6,7];\n\n\/\/printing the array\nconsole.log(ogArray);\n\n\/\/creating a new array to store elements in reverse order\nlet reversedArray=[];\n\n\/\/using the for() loop\nfor(let i=ogArray.length-1;i&gt;=0;i--){\n    \/\/adding elements in reversedArray\n    reversedArray.push(ogArray[i]);\n}\n\n\/\/printing the reversed ogArray\nconsole.log(reversedArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 4, 5, 6, 7 ]\n&#91; 7, 6, 5, 4 ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we iterate over the original array using a for loop from the last element to the first element (i.e. in the reverse order) and one by one add each element to the new array. We obtain a new array with reversed elements from the original array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using the unshift() function<\/strong><\/h3>\n\n\n\n<p>The unshift() function is used to add elements at the beginning of an array. The syntax of the unshift function is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>array_name.unshift(element);<\/code><\/pre>\n\n\n\n<p>where\u00a0array_name is the name of the array on which unshift() has to be performed\u00a0and element is the element that has to be added to the beginning of the array.<\/p>\n\n\n\n<p><strong>We will iterate over every element of the array and use this function for every element from first to last. At every iteration, the current element of the original array will be pushed at the beginning of the reversed array.<\/strong> At the final iteration, we will observe that the last element of the original array will come at first of the reversed array. <\/p>\n\n\n\n<p>Check this example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[4,5,6,7];\n\n\/\/printing the array\nconsole.log(ogArray);\n\n\/\/creating a new array to store elements in reverse order\nlet reversedArray=[];\n\n\/\/using the unshift() loop\nfor(let i=0;i&lt;ogArray.length;i++){\n    \/\/adding elements in the reversedArray using\n    reversedArray.unshift(ogArray[i]);\n}\n\n\/\/printing the reversed ogArray\nconsole.log(reversedArray)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 4, 5, 6, 7 ]\n&#91; 7, 6, 5, 4 ]\n<\/code><\/pre>\n\n\n\n<p>We have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using the reduce() function<\/strong><\/h3>\n\n\n\n<p>JavaScript&#8217;s reduce() method provides another powerful way to reverse an array. This method applies a function to each element in the array, accumulating the result in a single value. Check how to do it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/creating a custom function for reversing array using reduce\nfunction reverseArray(ogArray) {\n    \/\/using reduce function\n    return ogArray.reduce((accumulator, currentValue) =&gt; {\n      accumulator.unshift(currentValue);\n      return accumulator;\n    }, []);\n}\n \n\/\/declaring an array\nlet ogArray=[4,5,6,7];\n\n\/\/printing the array\nconsole.log(ogArray);\n\n\/\/declaring another array and using the custom function\nlet reversedArray=reverseArray(ogArray);\n\n\/\/printing the reversedArray\nconsole.log(reversedArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 4, 5, 6, 7 ]\n&#91; 7, 6, 5, 4 ]\n<\/code><\/pre>\n\n\n\n<p>In this example, the reverseArray function takes an array as input and uses the reduce() method to iterate through the elements of the input array. The reduce() method applies a function that takes two arguments: the accumulator and the currentValue.<\/p>\n\n\n\n<p>Inside the function, we use the unshift() method to add the currentValue to the beginning of the accumulator array, effectively reversing the order of the elements. Finally, we return the accumulator array as the result of the reduce() method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we took a deep dive into various methods to reverse an array in JavaScript. Try using these methods with different examples to understand them better.  You can check out <a href=\"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/\">Convert Array to String in JavaScript<\/a> etc to learn about arrays in detail.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about the various methods to reverse an array in javascript including the reverse() method, for loop, unshift, and reduce functions.<\/p>\n","protected":false},"author":9,"featured_media":680,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,13],"class_list":["post-677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/677\/revisions\/812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/680"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}