How to reverse an array using array.reverse in JavaScript? 

How to reverse an array in JavaScript using the built-in Array.prototype.reverse() method?

Well, stick around because this article provides a detailed explanation of the reverse() method, including its syntax, description, and examples of usage.

Apart from that, you’ll also discover alternative ways how to reverse an array using several methods in JavaScript.

What is JavaScript reverse array?

The Array.prototype.reverse() or the Array.reverse() method is a built-in function in JavaScript that reverses the order of the elements in an array.

This method changes the original array and returns a reference to the same array.

Syntax

arr.reverse()

Where arr is the array that you want to reverse. This method reverses the order of the elements in the array in place, meaning that it changes the original array.

Return value

The reverse() method returns a reference to the same array after it has been reversed.

How to reverse an array using array.reverse in JavaScript? 

The Array.prototype.reverse() method is the most straightforward way to reverse an array in JavaScript.

This method reverses the order of the elements in an array in place, meaning that it changes the original array and returns a reference to the same array.

Here’s an example of how to use this method:

let sampleArray = ["Math", "English", "Science"];
sampleArray.reverse();

console.log(sampleArray); 

Output:

[ 'Science', 'English', 'Math' ]

In our example, we have an array called sampleArray with three elements: “Math,” “English,” and “Science.” Then, call the reverse() method on sampleArray, which reverses the order of its elements.

When we log sampleArray to the console, we can see that its elements are now in reverse order: “Science,” “English, and “Math.”

Different ways on how to reverse an array in JavaScript?

Solution 1: Use a loop to swap elements

Another way to reverse an array is by using a loop to swap the elements. This can be done using a temporary variable or by using the XOR swap algorithm.

Here’s an example of how to make an array reverse in JavaScript using a loop and a temporary variable:

function reverseArray(arr) {
  let left = 0;
  let right = arr.length - 1;
  while (left < right) {
    let temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
    left++;
    right--;
  }
  return arr;
}

let sampleArray = ["Math", "English", "Science", "Programming"];
reverseArray(sampleArray);

console.log(sampleArray);

In this example, we have a function called reverseArray that takes an array as an argument. Inside the function, we use a while loop to swap the elements of the array.

We start with two pointers, one pointing to the first element (left) and one pointing to the last element (right). We then use a temporary variable (temp) to swap the values of these two elements.

We increment left and decrement right until they meet in the middle, at which point all the elements have been swapped and the array is reversed.

Output:

[ 'Programming', 'Science', 'English', 'Math' ]

Solution 2: Use the Array.prototype.reduce() method

The reduce() method can also be used to reverse an array by accumulating the elements of the original array in reverse order into a new array.

Here’s an example of how to use this method:

let sampleArray = ["Math", "English", "Science", "Programming"];
let reversedArray = sampleArray.reduce((acc, curr) => [curr, ...acc], []);

console.log(reversedArray);

Output:

[ 'Programming', 'Science', 'English', 'Math' ]

Solution 3: Use the spread operator and Array.prototype.reverse()

You can also use the spread operator (…) to create a copy of the original array and then call the reverse() method on the copy.

This way, you can reverse the order of the elements without changing the original array.

Here’s an example of how to do this:

let sampleArray = ["Math", "English", "Science", "Programming", "History"];
let reversedArray = [...sampleArray].reverse();

console.log(reversedArray); 

Output:

[ 'History', 'Programming', 'Science', 'English', 'Math' ]

Solution 4: Use Array.prototype.map()

You can also use the Array.prototype.map() method to reverse an array in JavaScript.

Here’s an example of how to do this:

let sampleArray = ["Math", "English", "Science", "Programming", "History"];
let reversedArray = sampleArray.map((_, i, arr) => arr[arr.length - 1 - i]);

console.log(reversedArray);

Output:

[ 'History', 'Programming', 'Science', 'English', 'Math' ]

Solution 5: Use recursion

You can also reverse an array using recursion.

Here’s an example of how to do this:

function reverseArray(arr) {
  if (arr.length === 0) {
    return [];
  } else {
    return [arr.pop()].concat(reverseArray(arr));
  }
}

let sampleArray = ["Math", "English", "Science", "Programming", "History"];
let reversedArray = reverseArray(sampleArray);

console.log(reversedArray);

In this example, we have a function called reverseArray that takes an array as an argument and returns a new array with the elements in reverse order.

The function uses recursion to achieve this: if the input array is empty, it returns an empty array; otherwise, it removes the last element from the input array using the pop() method, concatenates it with the result of calling reverseArray on the remaining elements of the input array, and returns the result.

Output:

[ 'History', 'Programming', 'Science', 'English', 'Math' ]

Conclusion

In conclusion, this article provides different approaches to reverse an array in JavaScript.

We introduce the built-in Array.prototype.reverse() or array.reverse method, which is the simplest way to reverse an array in JavaScript, modifying the original array in place.

Also, we provide alternative methods to achieve the same result, including using loops, the reduce() method, the spread operator, map(), and recursion.

Each approach is demonstrated with clear examples for better understanding. Whether you prefer simplicity or exploring different techniques, this article equips you with the knowledge to effectively reverse arrays in JavaScript.

We are hoping that this article provides you with enough information that helps you understand the JavaScript reverse array.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment