JavaScript modes
Summarize
Summary of JavaScript modes
JavaScript mode in ServiceNow defines the design and runtime settings for custom applications and scripts, controlling the accessible JavaScript features. There are three modes aligned with different ECMAScript standards:
Show less
- ECMAScript 2021 (ES12)
- ES5 Standards
- Compatibility
New scoped applications default to ECMAScript 2021 (ES12) mode, while new global applications default to ES5 Standards mode. Additionally, ES12 mode can be enabled on individual scripts within applications set to ES5 Standards or Compatibility mode.
JavaScript Modes Explained
ECMAScript 2021 (ES12) mode
This is the default for new scoped applications and new scripts, independent of the application’s overall JavaScript mode. It supports a modern subset of ECMAScript 2021 and 2022 features such as:
- Default function parameters
- Rest parameters and for-of loops
- Template literals and destructuring
- Const, let declarations, arrow functions, and class declarations
- Map, set data structures
- Optional chaining operator (?.)
This mode does not preserve legacy pre-Tokyo engine behaviors or support global scripts.
ES5 Standards mode
This is the default for new global applications and an option for scoped applications. It supports ECMAScript 5 syntax and features, including:
- "use strict" declaration
- Control over object extensibility and property characteristics
- Getters and setters on objects
- New Array and Date methods
- Native JSON support
- Compatibility with modern third-party libraries like lodash.js and moment.js
This mode does not preserve behaviors from the pre-Helsinki engine.
Compatibility mode
Used for scripts developed before ES5 Standards mode was introduced. It includes some differences from the previous JavaScript engine:
- Uses ES5 native JSON for JSON.stringify() and JSON.parse()
- Legacy JSON().encode() and JSON().decode() methods still supported but recommended only for legacy needs
- Third-party JavaScript libraries are not supported
Practical Considerations
Changing the JavaScript mode for an application or script can alter script behavior. It is important to review behavior changes and test thoroughly when switching modes to avoid issues.
Configuration Guidance
- Set JavaScript mode for an application: Define which ECMAScript features are available by selecting the appropriate JavaScript mode at the application level.
- Enable ECMAScript 2021 (ES12) mode for individual scripts: Use this to apply the latest JavaScript features in scripts within applications running ES5 Standards or Compatibility mode.
JavaScript mode is a design and runtime setting for custom applications and scripts. To support existing server-side scripts and new scripts developed to the ECMAScript 2021 standard, the JavaScript engine has three modes: ECMAScript 2021 (ES12), ES5 Standards, and Compatibility.
The JavaScript mode controls which JavaScript features you have access to in an application or script. The default mode for new scoped applications is ECMAScript 2021 (ES12) and for new global applications, it’s ES5 Standards. You can also turn on ECMAScript 2021 (ES12) mode for individual scripts in applications that use ES5 Standards or Compatibility mode.
For more information about features supported by the ECMAScript 2021 (ES12) and ES5 Standards modes, see JavaScript engine feature support.
ECMAScript 2021 (ES12) mode
ECMAScript 2021 (ES12) mode is the default mode when you create new scoped applications. When you create new scripts, ECMAScript 2021 (ES12) mode is turned on by default regardless of the JavaScript mode configured for the application. This mode doesn’t preserve the legacy behaviors in the pre-Tokyo JavaScript engine or work with global scripts.
- Default function parameters
- Rest parameters
- For-of loops
- Template literals
- Destructuring
- Declarations
- Assignment
- Parameters
- Const declaration
- Let declaration
- Arrow functions
- Class declarations
- Map set
- Optional chaining operator
(?.)
To learn about specific ECMAScript 2021 (ES12) features, see the Let's Learn ECMAScript 2021 videos on the ServiceNow Dev Program YouTube channel.
ES5 Standards mode
ES5 Standards mode is the default mode for global applications and is an option for scoped applications. This mode doesn’t preserve the legacy behaviors in the pre-Helsinki JavaScript engine.
- The "use strict" declaration
- Control over extensibility of objects
- Get and set properties on objects (accessors)
- Control over writability, configurability, and enumerability of object properties
- New Array and Date methods
- Native JSON support
- Support for modern third-party libraries such as lodash.js and moment.js
Compatibility mode
Compatibility mode is used for all scripts developed prior to the addition of ES5 Standards mode. Compatibility mode has some differences from the previous JavaScript engine.
- JSON.stringify() and JSON.parse() are implemented using the ES5 Native JSON object.
- The new JSON().encode() and new JSON().decode() are still supported, but should only be used when the legacy behavior is required.
The use of third-party JavaScript libraries isn’t supported in Compatibility mode.