Write Google Scripts using Modern JavaScript ES6


Google Apps Script has made the switch to Chrome V8 JavaScript Engine. You can now write your Google Scripts and GSuite addons directly in ES6 but with a few caveats.

Published in: Google Apps Script - Code

Google Apps Script has received a significant upgrade ever since it was first released to the public more than a decade ago. Apps Script now uses the V8 JavaScript Engine - it is the same runtime that is used inside the Google Chrome browser and the popular Node.js environment.

This essentially means:

  • Developers can write code using modern JavaScript syntax like Arrow Functions, Classes, Array Destructuring, Template Literals and more.
  • The V8 Engine is fast, powerful and continuously improving, It will likely improve the performance and memory utilization of your Google Scripts.
  • Developers can use new JavaScript ES6 features like Symbols, Iterators, Generators, Promises, Maps, Sets and Proxies that weren’t available in the previous version of Google Apps Script.

Chrome V8 JavaScript Engine

Any new projects that you create inside the Google Apps Script editor automatically use the new V8 runtime.

V8 JavaScript Runtime in Google Apps Script

If you would like to upgrade any old project to use V8, go to the Run menu and choose “Enable new Apps Script runtime powered by V8.” If you do not see this option yet, add a new runtimeVersion field in your project’s manifest file with the value of V8. You can set the value to DEPRECATED_ES5 to switch to the old version that uses the Mozilla’s Rhino JavaScript engine.

Tip: Type script.new in your browser to quickly create a new Google Apps Script project in your browser. (Source)

ES6 Modules - The Missing Part

ES6 introduced the concept of modules in JavaScript which allows developers to write reusable code that is also easier to refactor and maintain. You can break your program into separate files (modules) and then import them into other modules using import-export statements.

The new Google Apps Script environment doesn’t support ES6 modules.

The other big change is that functions become available based on the sequence of files in the script editor. Let me explain.

Say your Apps Script project has a lot of files and you have created two functions of the same name but they are located in different files. Apps Script won’t complain but when you can call this function, the one that is defined in the bottom-most file of the project will be invoked.

Google Apps Script Modules

If you prefer the ease of working with ES6 Modules, the Apps Script Starter kit can help. You can write code locally inside Visual Studio Code, bundle the modules into a single file with Webpack and then push the bundle to the cloud automatically with Clasp.

The starter kit has also been updated to use the new V8 Runtime. Watch this YouTube video to learn how to develop with Google Scripts using the Starter Kit.

Performance - V8 vs Vanilla JavaScript

Eric Koleda writes - “The performance story is mixed. Vanilla JavaScript code (looping, math) runs faster, but calls to G Suite services (SpreadsheetApp, etc) run slightly slower. While not ideal, we’ve always recommended that performance-intensive apps are usually a better fit for other platforms. In general Apps Script is trying to optimize for ease-of-use, not throughput. If performance is critical to your use case then you may want to investigate Google Cloud Functions, etc.”

Learn Modern JavaScript ES6

Coming back to Javascript, I do have a few recommendations that will help improve your understanding of ECMAScript 6.

  • Understanding ES6 - This online book covers all the new features that have been added to the JavaScript language since ES6.
  • ES6 Udacity - A detailed video course that covers all aspects of ES6, complete with quizzes and doesn’t cost a penny.
  • Exploring ES6 - Deep dive into the core ES6 features with examples.
  • ES6+ Introduction - An interactive screencast tutorial series that provides a walk-through of the most important ES6+ features.
  • Mozilla Docs - MDN is the best reference site for JavaScript, including ES6.
  • If you prefer premium courses, check out the ones by Maximilian Schwarzmüller, Wes Bos and Stephen Grider.

Also see: The Best Online Teachers for Web Development

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Code

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube