Considerations for switching JavaScript modes
Summarize
Summary of Considerations for switching JavaScript modes
Switching JavaScript modes in ServiceNow can impact the behavior of your existing scripts. Understanding these differences is crucial before making changes or troubleshooting issues. The modes reflect the evolution from a lenient pre-ES5 environment (Compatibility Mode) to stricter ES5 Standards Mode, and finally the more feature-rich ECMAScript 2021 (ES12) mode.
Show less
Key Behavior Differences Across JavaScript Modes
- Arguments Object: In Compatibility Mode, arguments reflect parameter changes; in ES5 and ES12 strict modes, they do not and can throw errors.
- Boolean Overrides: Primitive booleans can be overridden in Compatibility Mode, while ES5 and ES12 enforce stricter protections and encourage explicit conditional handling.
- Exception for Syntax Errors: Syntax error handling improves from inconsistent runtime exceptions in Compatibility Mode to consistent, clear errors in ES5 and ES12.
- Increment/Decrement Operators: Behavior becomes clearer and more predictable in ES5 and ES12, with stricter rules applied in certain contexts like constants.
- Line Continuations: Allowed but discouraged in Compatibility and ES5 modes; ES12 offers template literals as a better alternative.
- Missing Semicolons: Compatibility Mode uses automatic semicolon insertion (ASI) which may cause unexpected behavior; ES5 and ES12 require explicit semicolons, enforcing cleaner syntax.
- Calling Non-existent Functions: Compatibility Mode throws ReferenceError; ES5 throws TypeError; ES12 throws EcmaError, improving error specificity.
- Non-existent Properties: All modes return undefined without errors when accessing missing properties.
- Numeric Literals: ES5 introduces stricter parsing; ES12 adds support for binary, octal, and BigInt literals.
- Reserved Keywords as Properties: ES5 and ES12 allow reserved keywords as property names without errors, unlike Compatibility Mode.
- Keywords let and yield: In Compatibility Mode, these are not keywords and can be used as identifiers; ES5 introduces let as a keyword and reserves yield in strict mode; ES12 treats both as keywords and disallows their use as identifiers.
Practical Implications for ServiceNow Customers
When switching JavaScript modes, expect stricter syntax enforcement, improved error messages, and changes in how certain language features behave—especially in strict mode contexts. These changes improve code reliability and maintainability but may require updating existing scripts to comply with stricter rules. Enhanced numeric literal support and better keyword handling in ES12 enable use of modern JavaScript features.
Review your scripts for use of arguments object modifications, boolean overrides, missing semicolons, and reserved keywords before switching modes. Testing scripts in the target mode helps identify and resolve issues early. Adopting ES5 or ES12 modes leads to more predictable and robust scripting within your ServiceNow applications.
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. |