How to reverse an array in JavaScript

by Janeth Kent Date: 18-05-2023 javascript

In this tutorial we are going to see how you can change the order of the elements of an array so that they are inverted. You could use a loop and reverse the array manually, but as we will see, in JavaScript there are much faster ways to achieve our goal:

To start, let's start from the following array which we'll call 'letters':

const letters = ['a', 'b', 'c', 'd','e'];

How to reverse the array itself


An effective method of reversion could be to use the reverse() method, which will alter the array itself on which it is executed. In this way, we could declare the array as a constant and would not need to reassign the result of the method:

const letters = ['a', 'b', 'c', 'd','e'];  

The result of the above function will be an array with the values ['e','d', 'c', 'b', 'a'].


How to get a new reverse array


If you want to assign the result to another variable or constant, you can use the spread operator... , so that we can assign the array elements to a new array:

const letters = ['e','d', 'c', 'b', 'a'];  
const reversedLetters = [...letters].reverse(); 

Thus, the constant letters will continue to hold the elements ['a', 'b', 'c', 'd', 'e'], while the constant reversedLetters will contain the elements ['e','d', 'c', 'b', 'a'].

There is yet another way by which you can invert an array and get it in a new variable without the need to use the spread operator. It consists of using the slice() method without passing any parameter, just before inverting the array:

const letters = ['a', 'b', 'c', 'd','e'];  
const reversedLetters = letters.slice().reverse();    


The constant letters will contain the elements ['a', 'b', 'c', 'd', 'e'], while the constant reversedLetters will contain the elements ['e', 'd', 'c', 'b', 'a']

by Janeth Kent Date: 18-05-2023 javascript hits : 4465  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.


Related Posts

The Payment Request API: Revolutionizing Online Payments (Part 2)

Part 2: Advanced Features and Best Practices   In the first part of this series, we explored the fundamentals of the Payment Request API and how it simplifies the payment experience. Now,…

The Payment Request API: Revolutionizing Online Payments (Part 1)

The Payment Request API has emerged as the new standard for online payments, transforming the way transactions are conducted on the internet. In this two-part series, we will delve into…

Let's create a Color Picker from scratch with HTML5 Canvas, Javascript and CSS3

HTML5 Canvas is a technology that allows developers to generate real-time graphics and animations using JavaScript. It provides a blank canvas on which graphical elements, such as lines, shapes, images…

How do you stop JavaScript execution for a while: sleep()

A sleep()function is a function that allows you to stop the execution of code for a certain amount of time. Using a function similar to this can be interesting for…

Mastering array sorting in JavaScript: a guide to the sort() function

In this article, I will explain the usage and potential of the sort() function in JavaScript.   What does the sort() function do?   The sort() function allows you to sort the elements of…

Infinite scrolling with native JavaScript using the Fetch API

I have long wanted to talk about how infinite scroll functionality can be implemented in a list of items that might be on any Web page. Infinite scroll is a technique…

Sorting elements with SortableJS and storing them in localStorage

SortableJS is a JavaScript extension that you will be able to use in your developments to offer your users the possibility to drag and drop elements in order to change…

What is a JWT token and how does it work?

JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…

Template Literals in JavaScript

Template literals, also known as template literals, appeared in JavaScript in its ES6 version, providing a new method of declaring strings using inverted quotes, offering several new and improved possibilities. About…

How to use the endsWith method in JavaScript

In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…

What are javascript symbols and how can they help you?

Symbols are a new primitive value introduced by ES6. Their purpose is to provide us unique identifiers. In this article, we tell you how they work, in which way they…

Callbacks in JavaScript

Callback functions are the same old JavaScript functions. They have no special syntax, as they are simply functions that are passed as an argument to another function. The function that receives…