0% found this document useful (0 votes)
34 views5 pages

Basics of Javascript in Service Now

The document provides an overview of JavaScript fundamentals used in ServiceNow, covering topics such as basic syntax, variables, data types, and arithmetic operators. It explains control structures like if statements, ternary operators, and switch statements, as well as various loop types including for, while, and do...while loops. Additionally, it discusses data type conversion and the use of special characters in strings.

Uploaded by

Rohan Ijaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

Basics of Javascript in Service Now

The document provides an overview of JavaScript fundamentals used in ServiceNow, covering topics such as basic syntax, variables, data types, and arithmetic operators. It explains control structures like if statements, ternary operators, and switch statements, as well as various loop types including for, while, and do...while loops. Additionally, it discusses data type conversion and the use of special characters in strings.

Uploaded by

Rohan Ijaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

JavaScript Fundamentals & Basics Used in ServiceNow


Notes:

• JavaScript: Programming language used for scripting in ServiceNow.


• Used for automating tasks, customizing forms, workflows, business rules.
• Basic syntax includes writing statements, ending with semicolon ;.
• Case sensitive language.

Example:
javascript
CopyEdit
var greeting = "Hello ServiceNow!";
gs.info(greeting); // Logs the greeting in system logs

2. Variables, Data Types & Arithmetic Operators


Variables:

• Containers to store data.


• Declare with var, let, or const.
• var = function-scoped, let and const = block-scoped.
• const = constant, value can't change.

Data Types:

• String: Text e.g. "Hello"


• Number: Integers or decimals e.g. 5, 3.14
• Boolean: True or False values true or false
• Null: Represents “no value”
• Undefined: Variable declared but no value assigned

Arithmetic Operators:

Operator Meaning Example


+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 6 / 3 = 2
% Modulus (remainder) 5 % 2 = 1
3. Data Type Conversion and Special Characters
Data Type Conversion:

• Convert one data type to another.


• Common functions:
o String(value) converts value to string.
o Number(value) converts value to number.
o parseInt(value) converts string to integer.
o parseFloat(value) converts string to decimal number.

Example:
javascript
CopyEdit
var num = "123";
var convertedNum = Number(num); // 123 as number

Special Characters in Strings:

• Use escape sequences to include special characters:


o \n = new line
o \t = tab space
o \\ = backslash
o \' = single quote
o \" = double quote

4. If Statement and Boolean Logic with Examples


If Statement Syntax:
javascript
CopyEdit
if(condition) {
// code if true
} else if(anotherCondition) {
// code if another condition true
} else {
// code if none true
}

Boolean Logic:

• && (AND): true if both true.


• || (OR): true if one true.
• ! (NOT): reverses true/false.

Example:
javascript
CopyEdit
var age = 20;
if(age >= 18 && age < 60) {
gs.info("Adult");
} else {
gs.info("Not adult");
}

5. Ternary Operator & Switch Statement Demonstration


Ternary Operator:

• Shorter form of if-else.


• Syntax: condition ? valueIfTrue : valueIfFalse

Example:
javascript
CopyEdit
var age = 18;
var status = (age >= 18) ? "Adult" : "Minor";
gs.info(status);

Switch Statement:

• Used to select one block from many.


• Syntax:

javascript
CopyEdit
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}

Example:
javascript
CopyEdit
var day = 2;
switch(day) {
case 1:
gs.info("Monday");
break;
case 2:
gs.info("Tuesday");
break;
default:
gs.info("Other day");
}

6. JavaScript Loops in ServiceNow


For Loop:

• Runs code a fixed number of times.

javascript
CopyEdit
for(var i = 0; i < 5; i++) {
gs.info(i);
}

For...in Loop:

• Loops over properties of an object.

javascript
CopyEdit
var obj = {a:1, b:2, c:3};
for(var key in obj) {
gs.info(key + ": " + obj[key]);
}

For...of Loop:

• Loops over iterable objects like arrays.

javascript
CopyEdit
var arr = [10, 20, 30];
for(var val of arr) {
gs.info(val);
}

While Loop:
• Runs while condition is true.

javascript
CopyEdit
var i = 0;
while(i < 3) {
gs.info(i);
i++;
}

Do...While Loop:

• Runs code once, then repeats while condition true.

javascript
CopyEdit
var i = 0;
do {
gs.info(i);
i++;
} while(i < 3);

You might also like