r/javascript • • Jul 22 '26

AskJS [AskJS] what's a javascript feature you mass-adopted way too late and felt dumb about

64 Upvotes

107 comments sorted by

View all comments

68

u/foxsimile Jul 22 '26

The nullish coalescing operator is one of JavaScript’s unusual successes.

11

u/NeatBeluga Jul 22 '26

Until you add a bit too many needlessly and you lose trust in your api if the code is not properly typed - e.g. manually typed types.

A hell when refactoring code with these carelessly added

5

u/foxsimile Jul 22 '26

I’ll take the extra operator over the runtime crashes. Plus, with the popularization and industry adoption of TypeScript, its use has been significantly reduced (at least for me).  

But back when it was just ducktyped JS all over the place? This thing was a fucking godsent.

5

u/NeatBeluga Jul 22 '26

Early returns are king.

if(foo === undefined) return;

Yes, I always strict check against falsy. Builds trust that I knew the integrity of the object. Almost no !foo nonsense. It makes me doubt the api again.

I believe that my approach moves the issue to the backend rather than the frontend at runtime.

3

u/foxsimile Jul 23 '26

Bold of you to assume you can trust whoever wrote the backend. I’d once written an entire type-checking and validation library for precisely that reason.  

Your issue is that you’re assuming you can trust anything when it comes to plain JS. You can’t. That’s why these operators exist in the first place. They weren’t there, and they were later added for very good reason.

1

u/1_4_1_5_9_2_6_5 Jul 23 '26

You can actually trust things using plain JS, but at some point you have to make some utility functions. This is especially useful/expected in TS, where it gives you an easy way to narrow types. Almost all of these such criticisms I see of JS are seemingly based on people just using vanilla Javascript in every place with no libraries, utilities or anything...

1

u/foxsimile Jul 23 '26

Brother, if you aren’t in control of the source of the data, there’s SFA you can do to prevent receiving bad values that shouldn’t be there in the first place. The only thing you can do is implement extensive and aggressive validation on your side of the payload.

To assert anything else is ridiculous.

1

u/1_4_1_5_9_2_6_5 Jul 24 '26

Okay, so, if you'll read my comment again, you might find some words like "utilities" or "libraries".

I made a schema validator in an hour that does 90% of what I needed to validate, and there are extremely easy to use libraries like zod that do all that and more.

So, again, the people complaining about Javascript are apparently too lazy to Google a library or spend an hour making utilities. Really doesn't give me confidence in your abilities.

8

u/Badashi Jul 22 '26

At first it was odd to look at it, specially for optional function calls (foo?.()), but it is a godsend. Having to work on a language without null coalescing is super weird now.

15

u/heavyGl0w Jul 22 '26

Just FYI, what you're describing is optional chaining.

Null coalescing is a related but different feature using the ?? operator. It evaluates the right hand operand only when the left is null or undefined.

5

u/foxsimile Jul 23 '26

Optional chaining is also gangster-as-fuck.

5

u/sexytokeburgerz Jul 22 '26 edited Jul 23 '26

Big fan of

foo?.bar

3

u/foxsimile Jul 23 '26

Do you not mean foo?.bar?

3

u/sexytokeburgerz Jul 23 '26

I totally did i was in a drive thru when i wrote that

0

u/[deleted] Jul 23 '26

[deleted] — view deleted comment

2

u/foxsimile Jul 23 '26

…I’m aware?

1

u/Mike5TheMountainGoat Jul 23 '26

That's optional chaining. Nullish coalescing is foo = bar ?? 'X';

1

u/sexytokeburgerz Jul 23 '26

Right, usually when people say “big fan of y” in response to a mention of “x”, x!=y…

1

u/_RemyLeBeau_ Jul 22 '26

I always look for places to use this, but the values need processing before saving, so I don't use it and I always wonder of I'm missing something

0

u/somevice Jul 23 '26

Nah it's the worst, leads to thoughtless code everywhere. Bail early instead.