JavaScript Essentials
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.](https://servicenowwithrunjay.com/wp-content/uploads/images/snwr-js-essentials.png)
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
- Open System Definition > Scripts – Background (more on this in Lesson 35.3).
- Paste the loop example above and click Run script.
- Look at the output. Do you see three hello messages?
- 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?
Please log in to share lesson feedback.
Have a question or doubt?
Ask here and get help from instructors and fellow learners.
Only logged-in learners can post.




