Introduction to JavaScript in ServiceNow

JavaScript is the primary scripting language used in ServiceNow for both client-side and server-side operations.

Important Note

ServiceNow uses JavaScript (ECMAScript 5) on the server-side and modern JavaScript (ES6+) on the client-side.

Variables and Data Types

Variable Declaration Examples
// Server-side (Business Rules, Script Includes)
var userName = 'John Doe';
var isActive = true;
var ticketCount = 42;

// Client-side (Client Scripts - Modern)
let currentValue = g_form.getValue('priority');
const MAX_ATTEMPTS = 3;
var legacyVariable = 'still works';

CSA & CAD Exam Questions (12 Questions)

What is the difference between == and === in JavaScript? Q1
CSA
  • A) No difference, they work the same way
  • B) == compares values only, === compares both value and type
  • C) === is faster than ==
  • D) == is deprecated in ServiceNow
Show Answer
Correct Answer: B
The == operator performs type coercion before comparing, while === checks both value and type. Example: 5 == '5' returns true, but 5 === '5' returns false. Always use === in ServiceNow for predictable results.
Which keyword is used to declare a variable in server-side scripts? Q2
CSA
  • A) let
  • B) const
  • C) var
  • D) All of the above
Show Answer
Correct Answer: C
Server-side scripts use ECMAScript 5, which only supports 'var'. Client-side scripts support 'let' and 'const' as they run in modern browsers.
What will typeof null return? Q3
CSA
  • A) "null"
  • B) "undefined"
  • C) "object"
  • D) null
Show Answer
Correct Answer: C
This is a JavaScript quirk. typeof null returns "object" due to a legacy bug. To check for null, use: if (value === null) or in ServiceNow: current.field.nil()
What is the correct syntax for multi-line comments? Q4
CSA
  • A) // Comment //
  • B) <!-- Comment -->
  • C) /* Comment */
  • D) # Comment #
Show Answer
Correct Answer: C
Multi-line comments use /* */. Single-line uses //. Both are valid in ServiceNow scripts.
Which method adds an element to the end of an array? Q5
CSA
  • A) array.push()
  • B) array.pop()
  • C) array.shift()
  • D) array.unshift()
Show Answer
Correct Answer: A
push() adds to end, pop() removes from end, unshift() adds to beginning, shift() removes from beginning.
What does Boolean("false") return? Q6
CAD
  • A) false
  • B) true
  • C) "false"
  • D) undefined
Show Answer
Correct Answer: B
Any non-empty string converts to true. Only false, 0, "", null, undefined, and NaN convert to false. Be careful when checking boolean fields in ServiceNow.
What is the output of '10' + 5? Q7
CSA
  • A) 15
  • B) "15"
  • C) "105"
  • D) Error
Show Answer
Correct Answer: C
The + operator performs string concatenation when one operand is a string. Result: "105". Use parseInt() or Number() to force numeric addition.
Which loop is best for iterating over object properties? Q8
CAD
  • A) for loop
  • B) while loop
  • C) for...in loop
  • D) do...while loop
Show Answer
Correct Answer: C
for...in is designed for iterating over object properties. Example: for (var key in obj) { }
What is the scope of a variable declared with 'var' inside a function? Q9
CAD
  • A) Global scope
  • B) Function scope
  • C) Block scope
  • D) Module scope
Show Answer
Correct Answer: B
'var' has function scope, accessible throughout the entire function. 'let' and 'const' have block scope (client-side only).
Which method converts a string to lowercase? Q10
CSA
  • A) string.lower()
  • B) string.toLowerCase()
  • C) string.toLower()
  • D) string.lowerCase()
Show Answer
Correct Answer: B
toLowerCase() converts to lowercase, toUpperCase() converts to uppercase. Use for case-insensitive comparisons in ServiceNow.
What will typeof [] output? Q11
CAD
  • A) "array"
  • B) "object"
  • C) "Array"
  • D) "list"
Show Answer
Correct Answer: B
Arrays are objects in JavaScript. Use Array.isArray() to check if a variable is an array.
What is an IIFE (Immediately Invoked Function Expression)? Q12
CAD
  • A) function() { }()
  • B) (function() { })();
  • C) function()() { }
  • D) immediate function() { }
Show Answer
Correct Answer: B
IIFE syntax: (function() { code })(); - Executes immediately and creates a private scope. Useful in ServiceNow to avoid global namespace pollution.

Key Takeaways

  • Use === for comparisons
  • Server-side uses 'var' only
  • typeof returns "object" for arrays and null
  • String + Number = String concatenation
  • for...in for object properties