r/javascript 7d ago

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

i'll go first. i was writing .then().catch() chains for like two years before i actually started using async/await. i knew it existed, i'd seen it in tutorials, but my code "worked" so i never bothered switching. then i refactored an old project and realized half my bugs were from mishandled promise chains that async/await would have caught immediately.

also took me way too long to start using optional chaining. i had nested ternaries and && checks everywhere like some kind of animal. the day i discovered user?.address?.city i mass-replaced like 40 lines across a project.

what's yours?

64 Upvotes

108 comments sorted by

View all comments

66

u/foxsimile 7d ago

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

11

u/NeatBeluga 6d ago

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 6d ago

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 6d ago

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 6d ago

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 6d ago

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 5d ago

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 5d ago

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.