Menu Navigation Menu

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

proposal link

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.

let dateTimeInChicago = new CivilDateTime(2000, 12, 31, 23, 59)
let instantInChicago = dateTimeInChicago.withZone('America/Chicago');
let instantInSydney = new ZonedInstant(instantInChicago.instant, 'Australia/Sydney')
let dateTimeInSydney = instantInSydney.toCivilDateTime()
dateTimeInChicago.toString() // 2000-12-31T23:59:00.000000000
dateTimeInSydney.toString()  // 2001-01-01T16:59:00.000000000

Array.lastItem

proposal link

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:

[ 1, 2, 3, 4 ].last() # => 4

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.

function last(arr) {
  return arr[arr.length - 1] // OR
  return Object.assign([], arr).pop() // OR
  return arr.reverse()[0]
}

This proposal will allow us to use a getter property, someArray.lastItem, to get the last element of an array.

[ 1, 2, 3, 4 ].lastItem // => 4

Class Fields

proposal link

If you’ve written Java, you’re familiar with class definitions that have instance variables defined on them—maybe something that looks like this:

public class Person {
    private String name = "";
    private int age = 0;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

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:

class Person {
  name = ""
  age = 0

  constructor(name, age) {
    this.name = name
    this.age = age
  }
}

(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!


Contact us for a complimentary 30 minute consultation.

get in touch