Erick Wendel Profile picture
JS+Node.js specialist, and entrepreneur | staff software engineer @nodesource, @Microsoft MVP, @Google DevExpert | 🗣️ pt-br 🇧🇷, en-us 🇺🇸 and es-co 🇨🇴

Aug 4, 2022, 11 tweets

wow JavaScript is on 🔥with a lot of new functions for Arrays in progress 🤯😬 Welcome Immutability, Grouping, and Async helpers ❤️

🧵 /1

I'd say JavaScript makes my work as a content creator so easy sometimes 😂

There are so many new proposals and new features coming to the language that we could discuss a whole week about it haha.

/2

Today, I brought something interesting that I was missing for a while: Immutability in arrays!

/3

Did you know that most of the common functions in Arrays actually mutate their values?

const items = [ 1, 3, 2 ]

items.sort() // [1, 2, 3]
items // the value was actually changed by [1, 2, 3]

items.reverse() // [3, 2, 1]
items // now it's [3 , 2, 1]

/4

Or even worse, when you want to remove an item for a specific task using .splice (not .slice) and the whole list is affected:

items.splice(2, 2) // [ 1 ]
items // now it lost all items and kept only [ 1 ]

/5

The "Change Array by copy" proposal provides additional methods on Array.prototype to enable changes on the array by returning a new copy of it with the change.

Using the new set o features, the actual array won't change when you perform that kind of task 🤯❤️

/6

Not just to "fix" the language it has feature proposals but also to extend functionalities.

/7

I remember trying to group values on an object by doing the following:

let mapped = { even: [], odd: [] }
;[0, 1, 2, 3].forEach(num => num % 2 == 0 ?
mapped.even.push(num) :
mapped.odd.push(num)
)
mapped
// { even: [ 0, 2 ], odd: [ 1, 3 ] }

/8

Using the "Array Grouping" proposal you'd write everything on a single line:

[0, 1, 2, 3].group(num => num % 2 === 0 ? 'even' : 'odd');
// { even: [ 0, 2 ], odd: [ 1, 3 ] }

How amazing is it?🤩🤯

/9

And last, but not least, the "Array.fromAsync" proposal came to help us build arrays from Async Iterators 😬🤯

All of these features you can find on the @TC39 website and GitHub repo to follow the progress and the expected dates for them to finally become part of the lang

/10

Tell me here in the comments, what feature did you like most? What kind of function do you think JavaScript is still missing?

#javascript #tc39 #educators #100daysofcodechallenge #newfeatures #js #ecmascript #tipsandtricks /11

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling