Considerations for switching JavaScript modes
Summarize
Summary of Considerations for switching JavaScript modes
Switching JavaScript modes in your ServiceNow environment—from Compatibility Mode through ES5 Standards Mode to ECMAScript 2021 (ES12)—can significantly impact script behavior. Understanding these differences is crucial before changing modes or troubleshooting related issues, as changes affect script execution, error handling, and language features.
Show less
Key Behavioral Differences Across JavaScript Modes
- Arguments Object: In Compatibility Mode, modifications to arguments reflect in parameters; strict modes prevent this and may throw errors.
- Boolean Overrides: Primitive Booleans can be overridden in Compatibility Mode; ES5 and ES12 modes protect Booleans better, with strict mode preventing some assignments.
- Syntax Errors: More consistent and clearer syntax error handling was introduced starting in ES5 and improved in ES12, aiding debugging.
- Increment/Decrement Operators: Allowed on variables throughout, but ES5 and ES12 provide clearer, stricter behavior especially regarding constants.
- Line Continuations: Backslash line continuations behave similarly in Compatibility and ES5 modes, but ES12 encourages template literals for readability.
- Missing Semicolons: ES5 and ES12 enforce semicolon presence, throwing syntax errors to prevent issues common in Compatibility Mode.
- Calling Non-existent Functions: Different error types are thrown depending on mode, with ES12 providing more specific EcmaErrors.
- Accessing Non-existent Properties: Returns undefined consistently across modes without errors.
- Numeric Literals: ES5 introduced stricter parsing; ES12 supports binary, octal, and BigInt literals, expanding numeric capabilities.
- Reserved Keywords as Properties: Allowed in ES5 and ES12 (e.g., obj.for), whereas Compatibility Mode restricts this.
- Keywords let and yield: Not keywords in Compatibility Mode; let introduced in ES5; both are keywords in ES12, with use as identifiers causing syntax errors.
Implications for ServiceNow Customers
When upgrading JavaScript modes in ServiceNow applications or scripts, expect stricter syntax validation, improved error reporting, enhanced language features, and changes in how certain constructs behave. This can improve script reliability and maintainability but may require code adjustments to ensure compatibility.
Review existing scripts for constructs sensitive to mode changes—such as argument handling, Boolean usage, semicolon placement, and keyword usage—before switching modes. Testing scripts under the target mode helps identify and resolve issues proactively, ensuring smoother transitions and leveraging modern JavaScript capabilities effectively.
Switching the JavaScript mode for an application or script might change the behavior of existing scripts. Review some examples of behavior changes before switching JavaScript modes or to troubleshoot any issues that you experience after switching.
For more information about each JavaScript mode, see JavaScript modes and JavaScript engine feature support.
This table highlights how JavaScript behavior has evolved from the lenient and error-prone pre-ES5 environment, to the stricter and more predictable ES5, and lastly the more feature-rich environment of ES12 (ECMAScript 2021).
| Feature | Compatibility Mode | ES5 Standards Mode | ECMAScript 2021 (ES12) |
|---|---|---|---|
| Arguments object | The arguments object exists, but there's no strict
mode, so modifications reflect on arguments. Prints:
|
In strict mode, the arguments object doesn’t reflect parameter modifications and
throws an error. Prints: |
The same as ES5. |
| Boolean overrides | Primitive Booleans (true, false) can be overridden,
causing unexpected behavior. |
Primitive Booleans are more protected, though still can be overridden when assigned to variables. | The same as ES5, but strict mode helps prevent some assignments. The conditional
expression should be written in this form:
|
| Exception for syntax errors | Syntax errors throw exceptions at runtime. Error handling is inconsistent. Example:
|
More consistent syntax error handling, especially in strict mode. Example:
|
The same as ES5, but with more robust handling and clearer error messages in updated
engines. Example:
|
| Increment and decrement | Allowed on variables but could behave unexpectedly with complex expressions. Prints:
|
Improved clarity, but still allowed on variables (var,
let, const). Prints:
|
The same as ES5, with stricter rules in some contexts (for example,
const). |
| Line continuations | Allowed with a backslash (\) but discouraged due to readability
issues. In this example, all three functions are called.
|
Same as Compatibility mode; no change in handling line continuations. In the previous example, ES5 only calls the first function and treats everything after the first comment including the newline as comment until the expression end. | The same as ES5, but template literals provide a more readable alternative. |
| Missing semicolons | Automatic semicolon insertion (ASI) often led to unexpected behavior. | Throws a syntax error when a semicolon is missing. | The same as ES5. Updated practices encourage explicit semicolons. |
| Non-existent functions | Calling a non-existent function throws a ReferenceError. |
Throws a TypeError if a non-function is called. |
Throws an EcmaError when a non-existent function is called or a property is referenced. |
| Non-existent properties | Accessing a non-existent property returns undefined; no error
thrown. |
Same as pre-ES5. | The same as Compatibility mode and ES5 Standards mode. |
| Numeric literals | Basic decimal and hexadecimal literals. | Introduced stricter parsing rules and better handling of numeric literals. | Added binary (0b), octal (0o), and BigInt literals
(123n). |
| Reserved keyword as property | Using reserved keywords isn't possible. | Reserved keywords can be used as property names without error, for example,
obj.for. Prints the object when returned. |
The same as ES5. |
| Treat let and yield as keywords | let and yield aren’t keywords and can be used as
identifiers only. |
let is introduced as a keyword. yield is reserved in
strict mode. |
Both are keywords. Using them as identifiers throws syntax errors. |