5 features that will be in the next version of JavaScript (and are already in CoffeeScript)

11 Oct 2012 by Bart
Comment
JS.next features

The writing's on the wall. I mean code.

Over the past few years JavaScript has become a language that is equally as important to web development as any of the long established behemoths like PHP, Java or Ruby. What used to be a technology often associated with annoying pop-up windows, pointless web clocks and horrendous marquee text effects, is now responsible for high quality features present on almost every site. Indeed, the leap JavaScript has made is truly unbelievable with entire web servers now able to run on it, in-browser games being developed with it and all modern web services relying heavily on it.

However, with the rapid expansion of JavaScript and the growing complexity of programs written in it, the need for more mature programming practices has evolved as well with test-driven development in JS becoming a topic of real importance. Now we have great tools like Jasmine and Mocha to help with it and many excellent resources available to help embrace it, meaning JavaScript is most definitely the programming language you should learn next if you aren’t already familiar with it.

There are a few problems with JavaScript though, as you may already know, one of them being that it hasn’t changed much since it’s first release in 1995. This means JavaScript is almost as old as WWW which, in web terms, is ancient. Of course, there were a few major updates to the language but it’s syntax has remained pretty much the same over all these years. Even as the world has changed around it and peoples’ expectations have changed towards it, JS still looks the same as it did 10 years ago. Something is obviously wrong and that’s not just my personal opinion.

A few years ago, work on the new version of JavaScript (JS.next) started and one of their main goal is to improve code readability, promising some long awaited improvements to basic functionality and syntax, many of them inspired by CoffeeScript – a small meta language which compiles to JavaScript. I’ve been using CoffeeScript for the past two years and, as my affection for it has been growing steadily, I’m really looking forward to seeing some of the features I’ve already gotten so used to in CoffeeScript becoming natively part of JavaScript.

Of course the final spec for JS.next is still up in the air but there are few features we can be pretty certain about. Here’s a list of my top five feature that we’ll be seeing in the next version of JavaScript… and already exist in CoffeeScript.

1. Arrow functions

Arrow functions are one of the things I really love about CoffeeScript, a much shorter notation with improved readability and a clear example of how much of an inspiration CoffeeScript was to JS.next.

In the currently accepted proposal for JS.next functions are defined with a ‘fat arrow’ like so:

let identity = x => x

They can take single argument without parentheses and return object literals which is pretty much exactly how it works in CoffeeScript apart from the fact that CS requires parentheses for arguments and for regular functions you should use a single arrow:

identity = (x) -> x

Most likely JS.next will not adopt CoffeeScript’s two different function declarations of normal arrow (->) and fat arrow (=>) for dynamic this binding. (Probably good idea)

“I’ve been talking about better function syntax for a while. function is too long (I wish I’d used fn in 1995 and preempted this issue).”

Brendan Eich

2. Parameters assignment

A massive difference to function and variable declarations are among the first things you will notice the first time using CoffeeScript and if you don’t fall in love with these tiny additions I guess you should check yourself with your doctor. They are also features you should most definitely expect to find in JS.next.

a. Default values

Finally you can assign default argument values for a function. You can simply write:

ape = (name='Cesar') -> name.toLowerCase();

instead of:

var ape = function(name) {
  if (name == null) {
    name = 'Cesar';
  };
  return name.toLowerCase()
};

b. splats

If you ever worked with arrays of data and wanted to treat elements differently depending on what order they appear in the sequence then you could probably do it easier using splats. So far it does look like CoffeeScript’s implementation is a bit more flexible than the proposed version of splats for JS.next but what it means is that you can use constructs like

monkeys = [koko, bubbles, gordo, able, albert]
do_with = (first, second, rest…) ->
  chatWith(first);
  cuddle(second);
  sendToSpace(rest);
play_with(monkeys)

c. destructuring assignment

Have you ever had to define variables based on array elements or object methods? You can use one liners in CoffeeScript to do this and JS.next promises the same.

# given the object
gorilla = {name:"KoKo", location:"California", guardian:"Francine Patterson", skill: "Communication", date_of_birth:"July 4, 1971"}
# and function
plot_guardian_on_map = (monkey) ->
  {guardian, location} = monkey
  map.add_guardian(guardian, location)

3. Class declarations

The object orientated nature of JavaScript is one of its best features but prototypal inheritance can make anyone’s head ache after a few hours working on pure JavaScript as the syntax becomes convoluted at a certain level of complexity. We can expect JS.next to bring more CoffeeScript like inheritance syntax.

class Ape
  climb_tree: -> "I'm king of the jungle"

class Chimpanzee extends Ape

cheetah = new Chimpanzee
cheetah.climb_tree() #=> I'm king of the jungle

4. String interpolation

Lack of string interpolation is something that really makes me feel ill when I look at regular JS code nowadays and I really hope this one will get into the final version of JS.next in one way or another. Syntax isn’t crucial here and I’m sure hurdles like XSS and escaping can be overcome. CoffeeScript already allows it:

ape = {name : "Bubbles", race : "Chimp", age  : 25};
"#{ape.name} is a #{ape.age} years old #{ape.race}"

If code readability is a priority for next version of JS then this feature has to become available.

5. Loops

The next version of JS will almost certainly use better syntax for array iteration. It most likely won’t be as rich as the one in CoffeeScript but what’s already planned looks like a great improvement over what we have so far.

To iterate over the array CoffeeScript offers us:

for monkey in monkeys
  play_with(monkey)

and similarly with an object

primates = {chimp:"Bubbles", orangutan: "Clyde", gorilla:"KoKo"};
for (species,name) of primates
  return "Meet #{name}, the #{species}"

Overall, there’s much more to come from JS.next and plenty of really useful additions like modules, additional core object methods, binary data support and more. The things I mentioned above are just some of the planned syntax improvements to improve readability and ease writing.

CoffeeScript, of course, takes everything a bit further by adding Ruby/Python like syntax, which may not suite everyone but it does give a glimpse of what we can expect from the next version of JS. Likewise, the influence it’s had on the evolution of the spec for the new version of JavaScript is undeniable so if you haven’t tried CoffeeScript yet, I would highly recommend you to do so. After few initial humps it really starts to pay off, at least for us.

Photo credit: B.Boyds

If you liked this article then why not subscribe to our RSS feed.

Author: Bart Oleszczyk

Bart is a man of few words and occasional long blog posts.

Comments

Leave a Reply