2

JavaScript String Functions, Conditionals & Loops in ServiceNow

As we learned about variables and their data types, this topic focuses on how to use them. It focuses on string functions and how to handle text. Conditional and looping statements allow developers to make decisions and repeat actions. 

The following topic ensures an understanding of functions that appear in every ServiceNow script. From checking whether a field starts with a prefix to running logic for high-priority incidents, they also help process everyday task lists. 

What are String Functions? 

JavaScript strings provide methods (functions) to manipulate and format text. Manipulation never changes the original string; it returns a new one. Some common string functions include: 

  1. charAt(index): Returns the character at the given index
  2. concat(): Joins two or more strings
  3. indexOf(): Returns the first position of a value, or -1 if not found
  4. lastIndexOf(): Returns the last position of a value
  5. trim(): Removes spaces from the start and end
  6. toLowerCase(): Converts text to lowercase
  7. toUpperCase(): Converts text to uppercase
  8. slice(start, end): Returns the part between two positions (end not included)
  9. split(separator): Splits a string into an array
  10. startsWith(): Returns true if the string begins with the given text
  11. endsWith(): Returns true if the string ends with the given text

Statements in JavaScript

JavaScript statements are instructions the script engine follows to automate tasks, query data, and change form behaviours. 

Conditional Control Statements

Conditional Statements are used to control the execution flow. A specific condition is checked, and a block of code runs when it is true. Scripts decide using these statements what to do and when.

Statement Use it when
if One condition decides whether code runs
if…else You need two outcomes
if…else if You have several conditions or ranges (for example, priority < 3)
Nested if A second check depends on the first being true
switch You compare one value to a list of exact values

For example, an if statement can be used as: 

Syntax: 

if(expression/condition){ 

statement; 

} 

example: 

var age = 20; 

if(age>=18){ 

console.log("you are eligible to vote"); 

}

Looping Control Statements

A loop repeats a block of code as long as its condition stays true. Most loops have three parts: a starting point, a stopping condition, and a counter. In JavaScript, there are multiple ways of creating loops, the major ones are: 

for loop

It is used when developers know how many times they want to repeat a particular action. 

CODE: 

for (initialization; condition; counter/iterator) 

{ 

Statements; 

}

For example: 

function fun() { 

for(var i=1; i<=10; i++) { 

console.log(i); 

} 

} 

fun(); 

example: 

var str = "javascript"; 

for (var i = 0; i < str.length; i++) 

{

console.log(str[i]); 

}

while loop

Developers use it when they are not sure how many times to repeat. It executes the statement only when the condition is true. 

Syntax: 

initialization 

while(condition) 

{ 

Statements; 

Iterator/counter; 

}

For example: 

function f1(){ 

var i =0; 

while(i < 5){ 

console.log(i); 

i++; 

} 

}f1();

Jump Statements

These keywords abruptly change the flow of code execution in ServiceNow. There are three major jump statement types: 

  1. Return: These statements return and exit the current function immediately and optionally send a value back to the caller. After returning, whatever statements are there are ignored by the compiler.
  2. Break: These statements are used to stop a loop when a specific condition is met and are used simply by writing break wherever you want. 
  3. Continue: Unlike break, continue skips the current iteration and moves to the next step once a condition is met. 

Important: Always ensure that a condition becomes false at some point. If not, the loop never ends, becoming an infinite loop that can slow the instance and freeze transactions. 

What’s Next?

In the next topic, we will be focusing on how arrays, methods and objects work in Servicenow.

Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

SALE IS LIVE
50% OFF