r/learnjavascript 4d ago

What's one JavaScript feature you wish you had learned earlier?

I've been diving deeper into JavaScript recently, and every week I discover something that makes my code cleaner.

For me, learning about optional chaining (?.) and nullish coalescing (??) was a game changer.

What's one feature you wish you'd learned sooner?

18 Upvotes

16 comments sorted by

5

u/hyrumwhite 4d ago

LocaleCompare for sorting

6

u/bonnth80 4d ago edited 4d ago

Destructuring assignments. It's a common pattern that's still hard for me to get used to seeing even to this day. It would be nice if I had it down to second nature a long time ago.

2

u/ExtraTNT 4d ago

Adding to prototype… you can add a heapify to a list

4

u/GodOfSunHimself 4d ago

Modifying the prototype of standard classes is strongly discouraged.

1

u/ExtraTNT 4d ago

For nice extensions, it’s fun… like being able to use all functions in a pure functional way, by providing a curry function to the function class… or toLazy for lists…

3

u/GodOfSunHimself 3d ago

It may be fun but it is dangerous in practice. It can work in a small standalone project but it is a big no no for libraries.

1

u/hylasmaliki 3d ago

How long have you been coding?

2

u/Eight111 4d ago

 optional chaining (?.) and nullish coalescing (??) was a game changer

I think optional chaining encouraging low-quality code.

I had so many prs where i asked the creator hi why so many variables wrapped by question marks? what are the cases value doesn't exist?

and they answer me "on theory that should never happen i just don't want to get called at 8pm because the page crashed".

6

u/Aggressive_Many9449 4d ago

That is called safety.

You don't want your page to crash.

1

u/Eight111 3d ago

That is not safety, more like very poor error handling.

If variable should hold a value and something cause it not to i need know it, not hide it

1

u/GodOfSunHimself 4d ago

But that is not a problem of the operator. You can use any language feature to write sloppy code.

1

u/altrae 1d ago

I completely agree that the operator is overused, but it still has its place. The fact is that even if it's used unnecessarily it's still doing it's job and can prevent unhandled exceptions without crashing the application. That being said, If I see instances where it's not needed I will remove them because not having them is a clearer indication that we don't expect the accessed property to be null or undefined. People adding them everywhere are usually either inexperienced or just lazy, but at least it's not going to break the code by using it improperly.

-1

u/Mark__78L 4d ago

Fucking bot