Part of the Tips 'N Tricks" series.
Early on in my ServiceNow developer career, I would often use the JavaScript "alert()" method in Client Scripts or UI Policies while debugging some code. I quickly became annoyed with having to click on the OK button to close the popup window. Really annoyed when using multiple alert() statements.
So I switched over to using the "console.log()" and "console.warn()" methods. No more "OK clicks". I know, I'm lazy, but it's really about being productive. This way I can have the browser's console window open next to my main browser window and the messages will appear in that window without interrupting the flow of the form/catalog item/portal page/UI Page that I'm working on/testing.
Here's a sample Client Script that will output information to the console:
The only "issue" with using the console object is you get a little exclamation point warning icon in the script window, but that's all they are. Does not affect the code and you can save it without any problems. Not sure why the editor doesn't like the object.
And this is what appears in the console window:
The "warn" method adds a colored background and an icon beside the text. I like to add the "JC - " prefix so I can easily find the information as the window can get pretty crowded with other stuff:
With the use of multiple monitors or an ultra-wide one like I now use, having the console window open beside my main browser window is simple and easy, without any distractions/annoyances.
Oh, and one more benefit - if you forget to comment out a line of code that has an alert() in it, that line could be triggered and display something on-screen that your user's shouldn't see. At the very least, it does not look very good on you. But using console.log/warn, it's not quite so visible/embarrassing. 🤓
Here are links to information on some of the browser developer tools/console windows:
The example screenshots are from Chrome on macOS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.