Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
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:

 

find_real_file.png

 

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:

 

find_real_file.png

 

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:

 

find_real_file.png

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.

3 Comments