V8 runtime overview Stay organized with collections Save and categorize content based on your preferences.
In Apps Script and JavaScript, aruntime orruntime environment containsthe JavaScript engine that parses and executes script code. The runtime providesrules for how memory is accessed, how the program can interact with thecomputer's operating system, and what program syntax is legal. Each webbrowser has a runtime environment for JavaScript.
Historically, Apps Script has been powered by Mozilla's Rhino JavaScriptinterpreter. While Rhino provided a convenient way for Apps Script to executedeveloper scripts, it also tied Apps Script to a specific JavaScript version(ES5). Apps Script developerscan't use more modern JavaScript syntax and features in scripts using the Rhinoruntime.
To address this concern, Apps Script is now supported by theV8 runtime that powers Chrome and Node.js. You canmigrate existing scripts to V8in order to take advantage of the modern JavaScript syntax and features.
This page describes the new features enabled by V8 and how you can enable V8for use in your scripts.Migrating scripts to V8describes steps for migrating existing scripts to use the V8 runtime.
Features of the V8 runtime
Scripts that use the V8 runtime are able to take advantage of the followingfeatures:
Modern ECMAScript syntax
You can use modernECMAScriptsyntax in scripts that are powered by the V8 runtime. This syntax includeslet
,const
, and many other popular features.
SeeV8 syntax examples for a short list of popularsyntax improvements you can make using the V8 runtime.
Caution: ES6 modules are not yet supported.Improved function detection
Apps Script function detection is improved for scripts using V8. The newruntime recognizes these function definition formats:
functionnormalFunction(){}asyncfunctionasyncFunction(){}function*generatorFunction(){}varvarFunction=function(){}letletFunction=function(){}constconstFunction=function(){}varnamedVarFunction=functionalternateNameVarFunction(){}letnamedLetFunction=functionalternateNameLetFunction(){}constnamedConstFunction=functionalternateNameConstFunction(){}varvarAsyncFunction=asyncfunction(){}letletAsyncFunction=asyncfunction(){}constconstAsyncFunction=asyncfunction(){}varnamedVarAsyncFunction=asyncfunctionalternateNameVarAsyncFunction(){}letnamedLetAsyncFunction=asyncfunctionalternateNameLetAsyncFunction(){}constnamedConstAsyncFunction=asyncfunctionalternateNameConstAsyncFunction(){}varvarGeneratorFunction=function*(){}letletGeneratorFunction=function*(){}constconstGeneratorFunction=function*(){}varnamedVarGeneratorFunction=function*alternateNameVarGeneratorFunction(){}letnamedLetGeneratorFunction=function*alternateNameLetGeneratorFunction(){}constnamedConstGeneratorFunction=function*alternateNameConstGeneratorFunction(){}varvarLambda=()=>{}letletLambda=()=>{}constconstLambda=()=>{}varvarAsyncLambda=async()=>{}letletAsyncLambda=async()=>{}constconstAsyncLambda=async()=>{}
Call object methods from triggers and callbacks
Scripts using V8 can call object methods and class static methods from placeswhere you could already call library methods. These places include thefollowing:
- Google Workspace add-onsmanifest triggers
- Installable triggers
- Menu items in Google Workspace editors
- User callback functions, such the one described in the
ScriptApp.newStateToken()
code sample.
The following V8 example shows the use of object methods when constructingmenu items in Google Sheets:
functiononOpen(){constui=SpreadsheetApp.getUi();//OrDocumentApp,SlidesApp,orFormApp.ui.createMenu('Custom Menu').addItem('First item','menu.item1').addSeparator().addSubMenu(ui.createMenu('Sub-menu').addItem('Second item','menu.item2')).addToUi();}constmenu={item1:function(){SpreadsheetApp.getUi().alert('You clicked: First item');},item2:function(){SpreadsheetApp.getUi().alert('You clicked: Second item');}}
View logs
Apps Script provides two logging services: theLogger
service and theconsole
class. Both of these serviceswrite logs to the sameStackdriver Logging service.
To showLogger
andconsole
logs, at the top of the script editor, clickExecution log.
View executions
To view your script's execution history, open the Apps Script project and atthe left, clickExecutions
.Note: Since theExecutions panel doesn't provide timestamped logs of theindividual Apps Script service calls, it's recommended that you use theconsole
service to create appropriatelog messages. All logs created withconsole
appear in theExecutionspanel.V8 syntax examples
The following is a short list of popular syntactical features available toscripts using the V8 runtime.
let
andconst
Thelet
andconst
keywords allow you to define block scope local variables and block scopeconstants, respectively.
//V8runtimelets="hello";if(s==="hello"){s="world";console.log(s);//Prints"world"}console.log(s);//Prints"hello"constN=100;N=5;//ResultsinTypeError |
Arrow functions
Arrow functionsprovide a compact way of defining functions within expressions.
// Rhino runtimefunctionsquare(x){returnx*x;}console.log(square(5));// Outputs 25 | //V8runtimeconstsquare=x=>x*x;console.log(square(5));//Outputs25//Outputs[1,4,9]console.log([1,2,3].map(x=>x*x)); |
Classes
Classesprovide a means to conceptually organize code with inheritance. Classes in V8are primarily syntactical sugar over the JavaScript prototype-based inheritance.
//V8runtimeclassRectangle{constructor(width,height){//classconstructorthis.width=width;this.height=height;}logToConsole(){//classmethodconsole.log(`Rectangle(width=${this.width},height=${this.height})`);}}constr=newRectangle(10,20);r.logToConsole();//OutputsRectangle(width=10,height=20) |
Destructuring assignments
Destructuring assignmentexpressions are a quick way to unpack values from arrays and objects intodistinct variables.
//Rhinoruntimevardata={a:12,b:false,c:'blue'};vara=data.a;varc=data.c;console.log(a,c);//Outputs12"blue"vara=[1,2,3];varx=a[0];vary=a[1];varz=a[2];console.log(x,y,z);//Outputs123 | //V8runtimeconstdata={a:12,b:false,c:'blue'};const{a,c}=data;console.log(a,c);//Outputs12"blue"constarray=[1,2,3];const[x,y,z]=array;console.log(x,y,z);//Outputs123 |
Template literals
Template literalsare string literals that allow embedded expressions. They let you avoidmore complex string concatenation statements.
//Rhinoruntimevarname='Hi '+first+' '+last+'.';varurl='http://localhost:3000/api/messages/'+id; | //V8runtimeconstname=`Hi${first}${last}.`;consturl=`http://localhost:3000/api/messages/${id}`; |
Default parameters
Default parameterslet you specify default values for function parameters in the functiondeclaration. This can simplify the code in the function body as it removes theneed to explicitly assign default values to missing parameters.
// Rhino runtimefunctionhello(greeting, name){greeting=greeting||"hello";name=name||"world";console.log(greeting+" "+name+"!");}hello();// Outputs "hello world!" | //V8runtimeconsthello=function(greeting="hello",name="world"){console.log(greeting+" "+name+"!");}hello();//Outputs"hello world!" |
Multi-line strings
You can definemulti-line stringsusing the same syntax astemplate literals. As withtemplate literals, this syntax lets you avoid string concatenations and simplifystring definitions.
//Rhinoruntimevarmultiline="This string is sort of\n"+"like a multi-line string,\n"+"but it's not really one."; | //V8runtimeconstmultiline=`Thisontheotherhand,actuallyisamulti-linestring,thankstoJavaScriptES6`; |
Enabling the V8 runtime
If a script is using the Rhino runtime, you can switch it to V8 bydoing the following:
- Open the Apps Script project.
- At the left, clickProject Settings .
- Select theEnable Chrome V8 runtime checkbox.
Alternatively you can specify the script runtime directly byediting the script manifestfile:
- Open the Apps Script project.
- At the left, clickProject Settings .
- Select theShow "appsscript.json" manifest file in editor checkbox.
- At the left, clickEditor
appsscript.json
. > - In the
appsscript.json
manifest file, set theruntimeVersion
field to the valueV8
. - At the top, clickSave project .
Migrating scripts to V8 explainsother steps you should take to ensure your script functions well using V8.
Enabling the Rhino runtime
If your script is using V8 and you need to switch it to use the originalRhino runtime, do the following:
- Open the Apps Script project.
- At the left, clickProject Settings .
- Clear theEnable Chrome V8 runtime checkbox.
Alternatively, edit your script manifest:
- Open the Apps Script project.
- At the left, clickProject Settings .
- Select theShow "appsscript.json" manifest file in editor checkbox.
- At the left, clickEditor
appsscript.json
. > - In the
appsscript.json
manifest file, set theruntimeVersion
field to the valueDEPRECATED_ES5
. - At the top, clickSave project .
How do I migrate existing scripts?
TheMigrating scripts to V8guide describes the steps you need to take to migrate an existing script touse V8. This involves enabling the V8 runtime and checking the script forany known incompatibilities.
Automatic migration of scripts to V8
Starting February 18, 2020 Google will start gradually migrating existingscripts that pass our automated compatibility test to V8. The affected scriptscontinue to function normally after migration.
If you want to opt a script out of automatic migration, set theruntimeVersion
field in its manifest toDEPRECATED_ES5
. You can choose to manuallymigrate the script to V8 at anytime thereafter.
How do I report bugs?
TheSupport guide explains how to get programminghelp on Stack Overflow, search existing issue reports, file new bugs, andmake new feature requests.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-04 UTC.