Skip to tutorial content
Rate module Not yet rated

JavaScript Essentials

2 min read
On this page

In this lesson, you will learn:

  • Why ServiceNow admins need a little JavaScript
  • Variables, data types, and comments
  • If statements, functions, arrays, and loops

1 Why JavaScript?

ServiceNow scripts are written in JavaScript. You don’t need to be a developer to be a great admin. But knowing the basics lets you read scripts, fix small problems, and pass the CSA exam. πŸ’ͺ

Six JavaScript building blocks with small examples. Variables store a value: var name = 'Riya'. Data types include string, number, and boolean. If statements make decisions: if priority is 1, do something. Functions are reusable steps: function greet(name). Arrays hold lists: var teams = ['Network', 'Database']. Loops repeat steps: for each item in a list.
Figure 1. Six building blocks cover most admin scripts.

2 Variables, types, and comments

A variable is a labeled box that holds a value. Use var to create one.

// This is a comment. JavaScript ignores it.
var caller = 'Riya Sharma';   // string (text)
var priority = 1;             // number
var isActive = true;          // boolean (true or false)
Type Holds Example
String Text, in quotes 'VPN is slow'
Number Numbers, no quotes 4
Boolean true or false true

3 Making decisions: if

if (priority == 1) {
  gs.info('This is critical!');
} else {
  gs.info('Normal priority.');
}

πŸ“ Note: One = sets a value. Two == compares values. Mixing them up is the most common beginner bug!

4 Functions, arrays, and loops

A function is a set of steps you can reuse. An array is a list. A loop repeats steps for each item.

function greet(name) {
  return 'Hello, ' + name + '!';
}

var teams = ['Network', 'Database', 'Service Desk'];
for (var i = 0; i < teams.length; i++) {
  gs.info(greet(teams[i]));
}

πŸ’‘ Good to know: gs.info() writes a message to the log. It’s the easiest way to see what your script is doing.

⭐ Remember: var makes a variable. == compares. // starts a comment. Every { needs a }.

5 Let’s try it: run your first script

  1. Open System Definition > Scripts – Background (more on this in Lesson 35.3).
  2. Paste the loop example above and click Run script.
  3. Look at the output. Do you see three hello messages?
  4. Add 'Facilities' to the array and run it again.

6 Quick recap

  • ServiceNow scripts use JavaScript
  • Variables hold strings, numbers, or booleans
  • if makes decisions, functions reuse steps
  • Arrays hold lists, loops repeat steps
  • gs.info() writes to the log

7 Check yourself

Think of your answer first. Then tap to check.

1 What’s the difference between = and ==? Show answer

= sets a value. == compares two values.

2 Is ‘true’ (in quotes) a boolean? Show answer

No. In quotes, it’s a string. The boolean is true with no quotes.

3 Which method writes a message to the log? Show answer

gs.info().

Nice work! You finished this lesson. πŸŽ‰

Was this lesson helpful?

Have a question or doubt?

Ask here and get help from instructors and fellow learners.

0 / 300

Only logged-in learners can post.