We all love Javascript [citation needed], but if you use it a lot you’ve probably run up on small areas where the standard library doesn’t quite cut it. Maybe you’re looking through documentation for an Array#random
method (we don’t have it), or trying to find how to compare two dates in two different timezones (hope you’re using moment!) or learning there’s no builtin function to reverse a string (hint: make it an array first).
If you find yourself frustrated by all the little idiosyncrasies, it might be good to know that the kind folks that make our language spec are looking out for us. Here’s three upcoming language proposals to keep an eye on if you’re looking for small quality-of-life improvements.
Better Datetimes
The temporal proposal adds a bunch of niceties on top of the javascript baseline Date
object: for projects that want to sidestep having to write their own timezone-aware date comparison functions, but don’t need to import moment
quite yet.
For an example, here’s a snippet from that proposal’s README.
Array.lastItem
This is such a little thing, but I’m so excited for it. Lots of other dynamic languages have array methods that return the array’s last element, like ruby’s Array#last
:
Until now, though, Javascript hasn’t had that. We got around that by making our own functions to do it—i’ve seen mostly these three implementations out in the wild.
This proposal will allow us to use a getter property, someArray.lastItem
, to get the last element of an array.
Class Fields
If you’ve written Java, you’re familiar with class definitions that have instance variables defined on them—maybe something that looks like this:
You may also realize that Javascript doesn’t have that—we can’t define static class variables in the current iteration of the Javascript class
semantic. If you want to find out what instance variables classes use, you need to trace through the constructor and look through the code for assignments to this
.
With this proposal, though, we can list all our instance variables at the top of the class definition, like this:
(Never thought I’d be able to translate Java to Javascript so directly. What a wonderful world we live in in the year 2018.)
Even better news: most TC39 proposals have polyfills attached to them (sadly, not all-new syntax is unfortunately not something that can be polyfilled). If you’re really excited about one of these proposals, you can pull it down right now and get cracking. Be warned, though -polyfills for early-stage proposals tend to move very quickly as the proposal is evolving.
If you liked these proposals, there’s more where that came from; TC39, the Javascript standards committee, has all their active proposals in a repo. browse through, and let me know on Twitter what you’re excited about!