Blog

Javascript Brackets

Sometimes new programmers ask me what the different brackets are used for in ServiceNow Javascript.

Instead of the answer, "That is just how it is", (which isn't a good answer) I have now taken the time to write it down.

In Javascript:

Parentheses ( )

( ) are used to define function parameters

function hello(mike)

Parentheses () also define orders of evaluation.

// Evaluates 4 * 2 then adds 3 = 11
var answer = 4 * 2 + 3;
// Evaluates the 2 + 3 first (5) and then multiplies by 4 = 20
var answer = 4 * (2 + 3);

They are also required in while loops and for loops

for (var i=0; i < list.length; i++)
// Do something 'length' times
while (i < 10)
 i--;

Square brackets [ ]

[ ] are often used to define arrays. 

["Mike","James","Ted"]

Curly brackets, Squirrely brackets, or braces { }

{ } are used to open and close blocks of code. They declare when the function start and ends.

function hello(mike) {
gs.print('world');
}

They are also used in Jelly to declare parameters

<input type="hidden" id="incidentSysID" name="incidentSysID" value="${sysparm_sysID}"/>

They can also define objects in Javascript:

var person = { hair:"brown", eyes:"blue",name:"Mike Kaufman"};

Angle brackets or chevrons <>

<> are used in UI Pages for HTML tags 

<td>
<label>Create relationships:</label>
</td>

Hope that helps you with your future ServiceNow javascript!

Mike