If you've come to JavaScript after learning to program in other languages, one thing that's probably stuck in your craw over the years has been the lack of any way to define default parameters in functions. You've probably written something like this in the past:

view plain print about
1var foo = function (bar) {
2        bar = bar || 'ok';
3        // ...
4    };

For the most part that sorta thing probably worked out, until your argument was a boolean, which then really complicated things.

With ES2015, the heavens have opened and prayers have been answered, as we've finally been given the ability to define default parameters. Consider the following:

view plain print about
1// This is in a class
2    foo (bar=true) {
3        if (bar) {
4            console.log('First round is on Simon!');
5        } else {
6            console.log('No drinks today!');
7        }
8    }

Simple. We're saying that if bar is true, then Simon is buying, otherwise we're out of luck, and defaulting our argument to true. We can then call this method a few times to test it out:

view plain print about
1constructor () {
2        this.foo(); // 'First round is on Simon!'
3        this.foo(false); // 'No drinks today'
4        let bar;
5        this.foo(bar); // 'First round is on Simon!'
6    }

You can see from my comments what the output of those methods would be. It's important to note here that even the undefined value passed as an argument triggered the default argument value as well.

Hopefully default arguments will help you to significantly simplify your code in the future. As usual, if you have any feedback/praise/complaints/war stories, feel free to comment below, or drop me a private message through the "contact" link on the page.