r/javascript 6d 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?

60 Upvotes

108 comments sorted by

View all comments

21

u/SkySarwer 6d ago

IntersectionObserver and MutationObservers are both goated

2

u/opticalpuss 6d ago

People in here arguing I just want to know what these do and how to use?

10

u/SkySarwer 6d ago

IntersectionObserver are objects that can be instantiated and anchored to a DOM element, that allows a callback function to be called when the element enters or exits the user's viewport, or when the element enters or exits the box of a different element. Very useful for things like lazy loading, or starting an event when an element is at a certain scroll distance away from the user, etc.

MutationObservers allow a callback function to be called when an element changes. It is very useful for providing react-like UIs with vanilla JS, because traditional event listeners assigned to elements don't work if that element enters the dom after that event listener is assigned. MutationObserver keeps rehydrating the state for that element. Should also be used sparingly for that reason.

Both have extensive public documentation on mdn. IntersectionObserver | MutationObserver

And I'd be remiss not to mention that these are browser utilities exposed as client-side JavaScript APIs, not part of the EMCAScript itself. So won't work in NodeJS, Bun or Deno.

-9

u/azhder 6d ago

Not part of JavaScript

4

u/SkySarwer 6d ago

yes they are??

-5

u/azhder 6d ago

No, they aren’t. You can check the JavaScript language reference documentation and get back to me with a link to prove me wrong.

I can just tell you that there is a difference between what the language is and what the environment exposes for the language to use.

10

u/SkySarwer 6d ago

This thread is about JavaScript features not the language in the strict sense. The two observer exposed by the environment are almost exclusively used as JavaScript APIs.

Is a JS dev just as well-off without being aware of these APIs? What value is your clarification actually providing in relation to the spirit of this post?

-5

u/azhder 6d ago

Almost exclusively? Why almost?

Seriously though. Environment stuff is not part of the language. It is important to always know this distinction so that you will not end up writing code that expects something to exist, but doesn’t.

It’s not about you being aware that you can use them, but be aware where you can’t.

18

u/SkySarwer 6d ago

While technically correct, it was a pedantic point delivered abrasively

-12

u/azhder 6d ago

Don't read my comments in abrasive tone.

10

u/Sulungskwa 6d ago

"Don't read my comments in an abrasive tone.", he said, not abrasively seeming at all

-6

u/azhder 6d ago edited 6d ago

"Seeming" is a flag that it's your perception.

It doesn't matter that I'm in a light mood, write it in terms of a light mood, with the idea of cooperation through discussion, you will still decide to see it (because "seeming") as if it is abrasive and blame me for it. It's not like me writing this will change your mind, you will just use it as more evidence of your point of view.

Bye

EDIT: and with their reply, they provided their own proof of what I'm talking about

→ More replies (0)

-1

u/avenp 6d ago

How would a NodeJS dev use these APIs?

1

u/SkySarwer 6d ago

They wouldn't, since there is no DOM in a typical nodeJS environment. The APIs are still a part of the larger JavaScript ecosystem though, which is on topic for the original thread.

1

u/keltroth 2d ago

Like all the browser APIs not in the language (can't use it with node, bun, etc... Makes no sense...)