Back to JavaScript
2026-03-295 min read

REVERSE (JavaScript)

Learn REVERSE (JavaScript) step by step with clear examples and exercises.

Title: JavaScript REVERSE Function - A full guide

Why This Matters

In this lesson, we'll delve into the JavaScript reverse() function, a powerful tool that flips the order of elements in an array or string. Understanding how to use this function will help you solve real-world programming problems, ace coding interviews, and debug common mistakes in your code. This guide covers its usage, best practices, and common pitfalls.

Prerequisites

Before we dive into the reverse() function, it's essential to have a solid grasp of the following concepts:

  1. JavaScript fundamentals: variables, data types, functions, and arrays.
  2. Basic understanding of strings and their manipulation in JavaScript.
  3. Familiarity with array methods like push(), pop(), length, and slice().
  4. Knowledge of control structures such as loops and conditional statements.

Core Concept

The reverse() function is a built-in method in JavaScript that reverses the order of elements in an array or string. Here's how it works:

Array reverse() function

To use the reverse() function on an array, simply call it as a method on the array object:

let arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr); // Output: [5, 4, 3, 2, 1]

In this example, the reverse() function is applied to an array called arr, and it flips the order of elements from [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1].

String reverse() function

The reverse() function can also be used with strings. However, it modifies the original string and returns undefined. To preserve the original string, you should use the slice() method to create a new reversed string:

let str = "Hello World";
let reversedStr = str.split("").reverse().join("");
console.log(reversedStr); // Output: "dlroW olleH"

In this example, we split the original string into an array of characters using the split() method, reversed the order of elements with reverse(), and then joined them back together using join(""). The result is a reversed version of the original string.

When to use reverse()

The reverse() function is useful in various scenarios such as:

  1. Reversing the order of elements in an array or string for specific algorithms or data structures like stacks and queues.
  2. Debugging problems where you need to check if an array or string is reversed or not.
  3. Implementing custom sorting functions that require the input to be sorted in reverse order.
  4. Reversing the order of elements in a subarray or a specific range within an array using the slice() method.
  5. Creating custom iterators for traversing arrays in reverse order using the forEach(), map(), and reduce() methods.

Worked Example

Let's walk through a worked example using the reverse() function to solve a common problem: finding the palindrome status of a given string.

function isPalindrome(str) {
let reversedStr = str.split("").reverse().join("");
return str === reversedStr;
}

console.log(isPalindrome("racecar")); // Output: true
console.log(isPalindrome("hello")); // Output: false

In this example, we define a function called isPalindrome() that takes a string as an argument and returns true if the given string is a palindrome (reads the same forwards and backwards) and false otherwise. We use the reverse() function to create a reversed version of the input string, then compare it with the original string to determine if it's a palindrome.

Common Mistakes

When working with the reverse() function, some common mistakes include:

  1. Misunderstanding how the reverse() function works on arrays and strings.
  2. Forgetting to create a new reversed string when using the reverse() function with strings (instead of modifying the original string).
  3. Confusing the sort() function with the reverse() function, which sorts elements in ascending order by default.
  4. Failing to account for spaces and special characters when checking if a string is a palindrome.
  5. Assuming that the reverse() function will only reverse a specific part of an array or string without affecting the rest of it (e.g., using it on a subarray or a certain range within an array).
  6. Not considering edge cases such as empty arrays, strings with odd lengths, and strings containing non-alphanumeric characters.

Practice Questions

  1. Write a JavaScript function that reverses an array of numbers without using the built-in reverse() method.
  2. Implement a custom sorting function that sorts an array in reverse order using the sort() method.
  3. Write a JavaScript function that checks if a given string is a palindrome, including spaces and special characters.
  4. Use the reverse() function to implement a simple implementation of the "Last-In, First-Out" (LIFO) data structure using an array.
  5. Create a custom iterator for traversing an array in reverse order using the forEach(), map(), and reduce() methods.
  6. Write a function that reverses the order of words within a given sentence while maintaining the original sentence's structure (e.g., "Hello World" becomes "World Hello").
  7. Implement a recursive solution for reversing an array using the reverse() function and the stack data structure.
  8. Write a function that finds the middle element of an array, given an odd-length array, by reversing half of it and comparing with the other half.
  9. Create a JavaScript program that generates a random palindrome string containing a specified number of alphanumeric characters.
  10. Implement a function that checks if a given number is a palindrome (i.e., reads the same forwards and backwards).

FAQ

  1. Can I use the reverse() function on a mixed array of numbers and strings?
  • Yes, but be aware that the reverse() function will reverse the order of all elements in the array, including strings. If you want to maintain the original data types, consider using a separate approach or converting the array to an array of objects before reversing it.
  1. What happens if I call reverse() on an empty array?
  • Calling reverse() on an empty array will not change its contents and will return the array unmodified.
  1. Can I use the reverse() function with multi-dimensional arrays?
  • Yes, but keep in mind that the reverse() function will only reverse the elements at the current level of the array. To reverse all elements recursively, you'll need to implement a custom solution or use a library like lodash.
  1. How can I reverse the order of characters in a string without using the split(), reverse(), and join() methods?
  • You can create a custom function that swaps adjacent characters in the string until it's fully reversed. This approach, however, may be less efficient than using built-in methods like split(), reverse(), and join().
  1. Is there a performance difference between using reverse() and other array manipulation functions like push(), pop(), or shift()?
  • The reverse() function has a time complexity of O(n), while methods like push(), pop(), and shift() have a time complexity of O(1) for the operation itself but may have additional overhead when modifying the array's length. In practice, the difference in performance is usually negligible for small arrays, but it can become significant for very large arrays.
REVERSE (JavaScript) | JavaScript | XQA Learn