JavaScript Array sort() vs toSorted() (new in ES 2023)

preview_player
Показать описание
We all know that the JavaScript array sort() method sorts an array. So why do we need a toSorted() method?

toSorted() is new in ECMAScript 2023, also known as ES 14, which is the standard for JavaScript.

In this video, we look the array sort() and toSorted() methods. The sort() method sorts the array in place, mutating the original array. And toSorted() creates a new sorted array from the original array, treating the original array as immutable.

toSorted() is a great choice when working in applications that support immutable patterns such as state management libraries and functional programming techniques.

*Links*

*Content*
00:00 Array sort() vs toSorted()
00:36 Array sort() method is mutable
01:49 Issues with a mutable sort
02:43 Array toSort() method is immutable
03:02 toSort() supported in ES 2023 and TypeScript 5.2
03:59 Array toSort() method is immutable (continued)
05:01 Wrap Up
05:30 Setting up Angular to work with ES 2023

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
😊About Me
Hey! I'm Deborah Kurata
I'm a software developer and YouTube content creator. I speak at conferences such as VS Live and ng-conf. I write articles for freeCodeCamp. And I'm a Pluralsight author with courses in the top 10 most popular (out of 7,000+) over the past 5 years. For my work in support of software developers, I've been recognized with the Microsoft Most Valuable Professional (MVP) award, and I'm a Google Developer Expert (GDE).

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
#javascript #sort #tosorted #sortvstostored #es14 #es2023 #angularandes2023
Рекомендации по теме
Комментарии
Автор

Thank you for teaching this, really informative and helpful

adishjain
Автор

I found your tutorial to be very informative. Thank you for sharing it.

behzadkhosravanimajd
Автор

So it's a more prettier and concise way of doing :

const sortedAges = ages.slice().sort((a, b)=> a - b), am I correct?

icode
Автор

Or we can simply do ages.slice().sort((a, b) => a- b). Slice without params make a copy of the array.

timothyalcaide
Автор

You mentioned in your last two videos that mutating the original array causes problems with applications that support immutable patterns, but I can't imagine how this occurs?
Please can you explain it?

Eslam-iggf