If you have been using ServiceNow over the last few years, you may recall that the Javascript version was outdated. This meant certain functionality didn’t exist. Well, on most script fields, you now have the ability to enable ES12 (many of which may be active by default). This was added as of the Tokyo release.
Why is this important? Well, compared to ES6, ES12 adds the ability to use operators and calls that will decrease the amount of code you would have to write. You also have all the additional features from all the versions after ES6.
ES6 in ServiceNow doesn’t have all the features of ES6. It is more akin to ES5. This means you are getting a much larger suite of functionality with ES12.
A great example is function parameter defaults. I covered this previously, however, that was using ES6.
Here is the code from ES6:
exampleFunction: function(inputValue) {
inputValue = (inputValue) ? inputValue : ‘Default’
return inputValue;
}
With default parameters, if inputvalue is empty, we can do the following in ES12.
exampleFunction: function(inputValue = ‘Default’) {
return inputValue;
}
This is just one example of the differences available.
The inputValue one-line if/else statement in the ES5 script is nice. It is a common way to set values within scripts. But it can be shorter with ES12.
With ES12, we can do the following:
inputValue ||= ‘Default;
This is just scratching the surface of what is possible. Other capabilities such as function overrides, logic assignments, numeric separators, String.replaceAll(), Promise.any() and Private Class methods were added. If you want to step up your coding to the next level I recommend checking out more features that were added with ES12.
Next time, ill dive a little deeper on more use cases for ES12.
As a disclaimer, it is recommended to use ES12 Scripts in all scripts that are calling ES12 logic. This includes using ES12 Script on Business Rules that call to ES12 Script Includes. This ensures no issues occur between the two.
Lastly, while it will support ES12, it will have some items that may not work. You will have to do some testing yourself to find out exactly.
For more information on changes within ES6 to ES12, you can look at this link:
https://medium.com/@bluetch/javascript-es6-es7-es8-es9-es10-es11-and-es12-519d8be7d48c
ServiceNow documentation on ES12 being added:
https://docs.servicenow.com/bundle/xanadu-api-reference/page/script/JavaScript-engine-upgrade/concept/c_JS_modes.html