Using Es6 with Apps Script

At the time of writing, the Apps Script JavaScript engine is based on ES3 with a few useful ES5 things added. Much of the rest of the world is at Es6 with Es7 coming along. Much of Es6 needs new syntax that isn't supported by the Apps Script IDE, but a lot of the goodies can be emulated using a Shim which will allow you to use  them by translating them back into the earlier version of JavaScript. 

Paul Miller has a nice Es6-shim ready for browser use, so I made a couple of very minor tweaks to get it working with Apps Script, and here's the library that you can use to start to bring ES6 goodness into Apps Script while waiting for it to become native. The good thing about this approach is that when it does become native, you'll won't need to change a thing - just stop using the libraries. 

The library is cEs6Shim, and you'll find it here 1dvi84vwjD03YUc_-yp4D_fFjIXTK0J8Zk93qtNTPDI0xe2NO35XSv9em or on Github if you want to make your own copy. Right now it's not minimized but I'll add a minimized version at some future point.

What can you do with Es6.

There are lots of tutorials out there, so rather than repeat them here, you can go and take a look. Remember that any syntactical changes such as {...args} and for .. of won't be able to work in the Apps Script IDE, but most of the additional methods will and have been implemented in this library. Here's some examples following. In some cases I've shown how you can achieve the same thing in vanilla JavaScript.

Setting up

You need the library (or your own copy in your project). If you are using the library, you'll need a couple of local references for the Map and Set object if you plan to use them, as follows
  // need to import a couple of things if using as library
  // the rest are added to the prototypes
  var Set = cEs6Shim.Set;
  var Map = cEs6Shim.Map;

Array methods
var arr = ["a", "b" , "c","d","e"];

// same as arr.slice();
Logger.log (Array.from(arr));   // [a, b, c, d, e]

// same as split("");
Logger.log (Array.from ("Bruce")); // [B, r, u, c, e]

// same as (d) { return d.return d.toUpperCase(); });
Logger.log (Array.from (arr , function (d) { return d.toUpperCase(); })); //[A, B, C, D, E]

// can also convert array like to array
(function () {
  // same as (arguments);
  Logger.log (Array.from (arguments)); //[x, y]
}) ("x","y");

// same as [1,1].map(function(d) { return 0; });
Logger.log([1,1].fill(0)); //[0.0, 0.0]

// like indexOf but with function
Logger.log (arr.find (function (d) { return d ==="c" }));  // c
Logger.log (arr.findIndex (function (d) { return d ==="c" })); // 2.0

var arr2 = Array.from(arr);
// like splice
Logger.log (arr2.copyWithin (2,1, 3)); //[a, b, b, c, e]

// we can use Array.from to convert iterators to an array, since we dont have the for ... of syntax
Logger.log (Array.from (arr.keys())); //[0.0, 1.0, 2.0, 3.0, 4.0]
Logger.log (Array.from (arr.values())); // [a, b, c, d, e]

String methods
 // strings
  var str = "bohemian rhapsody";
  // same as str.slice (-2) === "dy"
  Logger.log (str.endsWith ("dy")); // true
  // same as str.indexOf ("ian") !== -1
  Logger.log (str.includes ("ian")); //true
  // same as new Array(11).join ("abc");
  Logger.log ("abc".repeat (10));  // abcabcabcabcabcabcabcabcabcabc
  // same as str.slice (0,2) === "bo"
  Logger.log (str.startsWith ("bo")); // true

// maps - a more convenient way to deal with kv pairs
  var map = new Map([['mystery','tour' ], ['iamthe', 'walrus']]);
  map.set('bungalow', 'bill');
  Logger.log(map.get('bungalow'));  // bill
  // cant use .delete method
  Logger.log(map.get('iamthe'));  // undefined
  // iterators need to the for .. of syntax but you can make them into an array
  Logger.log(Array.from(map.keys()));  // [mystery, bungalow]
  Logger.log(Array.from(map.values())); // [tour, bill]

// Sets
  var set = new Set(["yesterday", "yellow submarine"]);
  Logger.log (set.has ("madonna")); // true
  Logger.log (set.has ("lady madonna")); // false
  // cant use .delete
  set['delete'] ("yesterday");
  Logger.log (Array.from(set.values())); //[yellow submarine, lady, madonna]

For more like this, see Google Apps Scripts snippets. Why not join our community , follow the blog, twitter, G+ .You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.