DEV Community

Talia Hatfield
Talia Hatfield

Posted on

Webdev Quick Insights #1

Often, a newer JavaScript feature is not simply a replacement for an old one that you should always use in its place. You have to understand exactly what's going on.

The most basic, obvious example: When I first learned JavaScript, I was taught about var. When I told the new way was with let and const, I thought "OK, I'll just use let instead of var." I didn't realize that you should actually "default" to using const; I valued let because it was felt equivalent - like a drop-in replacement. Of course, there technically isn't a modern direct equivalent to var, because it's the behavior of var that is problematic. Embracing let and const involves changing your thinking.

While there is almost no reason to use var in a modern app, and maturity is just about knowing which "replacement" to use (let or const), there are many cases where the "old way" isn't totally outdated. For example, || and ?? are both potentially useful operators. ?? is newer and so could be considered "more modern", but that doesn't mean you should use it in any instances where you would use ||. Doing so could easily cause errors.

We all love low-hanging fruit. It's fun to easily improve code by employing a newer language feature. But doing so can take more care than you realize.

Top comments (0)