Brief
The to
method returns a new array in which the order of all elements is reversed compared to the original array: the first element becomes the last, and the last becomes the first.
Example
const planets = ['Mercury', 'Venus', 'Earth', 'Mars']const reversedPlanets = planets.toReversed()console.log(reversedPlanets)// ['Mars', 'Earth', 'Venus', 'Mercury']// The original array remains unchangedconsole.log(planets)// ['Mercury', 'Venus', 'Earth', 'Mars']
const planets = ['Mercury', 'Venus', 'Earth', 'Mars'] const reversedPlanets = planets.toReversed() console.log(reversedPlanets) // ['Mars', 'Earth', 'Venus', 'Mercury'] // The original array remains unchanged console.log(planets) // ['Mercury', 'Venus', 'Earth', 'Mars']
Syntax
Array
has no arguments.
Array
returns a new array in which the order of all elements is reversed compared to the original array.
Understanding
Arrays already have the reverse
method to obtain an array with the elements in reverse order. Unlike it, the to
method does not modify the original array.
Let's practice with cats:
const cats = ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat']console.log(cats.toReversed())// ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka']// The original array remains unchangedconsole.log(cats)// ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat']
const cats = ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat'] console.log(cats.toReversed()) // ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka'] // The original array remains unchanged console.log(cats) // ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat']
When using the reverse
method, the original array is also modified:
const cats = ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat']console.log(cats.reverse())// ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka']// The original array has changed!console.log(cats)// ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka']
const cats = ['Murka', 'Kuzya', 'Murr', 'Cheshire Cat'] console.log(cats.reverse()) // ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka'] // The original array has changed! console.log(cats) // ['Cheshire Cat', 'Murr', 'Kuzya', 'Murka']
If the elements of the original array are objects, the array returned by the to
method will contain references, not new objects. Thus, changes to an object in the original array will be visible in the created array and vice versa:
const versions = [ {name: 'ES6'}, {name:'ES2016'}, {name: 'ES2017'}]const result = versions.toReversed()console.log(result)// [{ name: 'ES2017'}, {name: 'ES2016'}, {name: 'ES6'}]// Change an element of the original arrayversions[0].year = 2015console.log(versions)// [{name: 'ES6', year: 2015}, {name: 'ES2016'}, {name: 'ES2017'}]// Changes are also visible in the created arrayconsole.log(result)// [{ name: 'ES2017'}, {name: 'ES2016'}, {name: 'ES6', year: 2015}]
const versions = [ {name: 'ES6'}, {name:'ES2016'}, {name: 'ES2017'} ] const result = versions.toReversed() console.log(result) // [{ name: 'ES2017'}, {name: 'ES2016'}, {name: 'ES6'}] // Change an element of the original array versions[0].year = 2015 console.log(versions) // [{name: 'ES6', year: 2015}, {name: 'ES2016'}, {name: 'ES2017'}] // Changes are also visible in the created array console.log(result) // [{ name: 'ES2017'}, {name: 'ES2016'}, {name: 'ES6', year: 2015}]
You can read more about the peculiarities of object copying in the article “Shallow and Deep Copying of Elements”.
Tips
💡 If the array has unfilled elements, to
will return undefined
as the value for all unfilled elements:
const poets = ['Emily Dickinson']poets[3] = 'Robert Frost'console.log(poets)// ['Emily Dickinson', <2 empty items>, 'Robert Frost']console.log(poets.toReversed())// ['Robert Frost', undefined, undefined, 'Emily Dickinson']
const poets = ['Emily Dickinson'] poets[3] = 'Robert Frost' console.log(poets) // ['Emily Dickinson', <2 empty items>, 'Robert Frost'] console.log(poets.toReversed()) // ['Robert Frost', undefined, undefined, 'Emily Dickinson']
💡 Support for the to
method in major browsers and in Node.js appeared relatively recently. For example, trying to use to
in Node.js v.18.19.0 will lead to an error:
try {console.log([1, 2, 3].toReversed())} catch (err) { console.log('Caught an error! Here it is: ', err.message)}// Caught an error!// Here it is: [1,2,3].toReversed is not a function
try { console.log([1, 2, 3].toReversed()) } catch (err) { console.log('Caught an error! Here it is: ', err.message) } // Caught an error! // Here it is: [1,2,3].toReversed is not a function