ServiceNow JavaScript Primer
ServiceNow JavaScript Primer
JavaScript Primer
JavaScript
Primer
Contents
Overview
........................................................................................................................................
3
The
Language
..................................................................................................................................
4
Syntax
..............................................................................................................................................................
4
Variables
and
Data
Types
................................................................................................................
6
Variables
.........................................................................................................................................................
6
Data
Types
.......................................................................................................................................................
6
Functions
........................................................................................................................................
6
Defining
a
Function
.........................................................................................................................................
7
Modularization
................................................................................................................................................
7
Readability
......................................................................................................................................................
7
If/Else
if/Else
................................................................................................................................
10
Comparison
Operators
..................................................................................................................................
10
Compound
Comparisons
...............................................................................................................................
10
If/Else
if/Else
.................................................................................................................................................
11
Review
..........................................................................................................................................
13
Syntax
............................................................................................................................................................
13
Variables
.......................................................................................................................................................
13
Functions
......................................................................................................................................................
13
If/Else
if/Else
.................................................................................................................................................
13
Exercises
.......................................................................................................................................
14
Exercise
1
Variables
....................................................................................................................................
14
Exercise
2
Functions
...................................................................................................................................
14
Exercise
3
If/Else
if/Else
.............................................................................................................................
14
Resources
.....................................................................................................................................
14
JavaScript
Books:
...........................................................................................................................................
14
New
to
JavaScript:
.........................................................................................................................................
14
Online
Resources:
.........................................................................................................................................
14
The
Language
Syntax
Basic
need-to-know:
EVERYTHING
is
case-sensitive
var
stringA
=
"String
A";
var
stringa
=
"String
a";
alert(stringA
==
stringa);
//
false
Use
as
much,
or
as
little
extra
whitespace
as
you
like.
var
stringA
=
"String
A";
var
string="String
B";
Semicolons
are
JavaScript's
equivalent
of
a
period.
After
you
complete
a
statement
(sentence),
end
it
with
a
";"
character.
When
defining
string
values
("this
is
a
string
of
characters"),
single
(')
and
double
(")
quotes
are
interchangeable,
as
long
as
the
closing
matches
the
opening.
Parentheses
are
used
for
2
reasons:
1. Checking
equality
(evaluating
2
or
more
items
to
simply
"true"
or
'false')
if
(stringA
==
stringa){
//
do
something
}
2. Sending
or
receiving
a
value
var
product
=
multiply(7,
3);
//
send
7
and
3
function
multiply(a,
b){return
a*b;}
//
receive
7
as
"a"
&
3
as
"b"
Comments
are
a
way
to
explain
to
others
(or
leave
reminders
for
yourself)
what
you're
doing,
and
are
denoted
one
of
two
ways:
1. Single-
line
comments
are
denoted
by
a
double
slash
"//"
//
This
is
a
friendly
reminder
var
product
=
multiply
(4,
6);
Functions
Functions
serve
2
major
purposes:
1. Make
reusable
code
(modularization)
2. Clean
your
code
for
legibility
Readability
As
of
now
we
haven't
seen
code
that
is
long
enough
to
be
difficult
to
read
or
follow,
but
when
your
scripts
get
longer,
you'll
run
into
the
readability
problem.
Here's
an
example
of
a
longer
piece
of
code
(in
ServiceNow,
this
is
the
out-of-box
VIP
caller
highlighting
script):
function
onChange(control,
oldValue,
newValue,
isLoading)
{
//wait
until
there
is
a
valid
record
in
the
field
if
(!newValue)
return;
if
(0
===
"0"){
alert('these
are
not
identical!');
//
won't
alert
}
Compound
Comparisons
The
above
is
all
well
and
good,
but
what
happens
if
you
want
to
compare
multiple
items
at
one
time?
This
looks
a
little
messy,
and
if
you
have
a
lot
of
conditions,
it
will
quickly
get
hard
to
read.
What
we
can
do
is
apply
Boolean
logic,
use
AND
("&&")
and
OR
("||")
operators
to
combine
conditions
to
this
result:
var
precipitation
=
true;
var
temperature
=
55;
if
(precipitation
==
true
&&
temperature
<=
60){
alert("It's
cold
and
wet
today!");
}
If/Else
if/Else
Now
that
we
understand
comparisons,
we
can
make
them
much
more
useful
by
adding
an
otherwise
clause
the
"else"
clause.
Above,
we
are
checking
to
see
if
the
weather
is
cold
and
wet,
but
we're
not
doing
anything
if
the
weather
is
NOT
cold
and
wet.
In
our
weather
info,
we
have
essentially
4
potential
results:
1. Both
cold
and
wet
2. Just
cold
3. Just
wet
4. Neither
Its
a
little
better
but
hard
to
follow
can
we
simplify
it
even
further?
Introducing:
the
"else
if"
statement.
With
the
else
if
statement,
we
can
develop
a
multi-position
switch
of
sorts,
only
checking
the
conditions
we
need:
var
precipitation
=
true;
var
temperature
=
55;
if
(temperature
<=
60
&&
precipitation
==
true){
alert("It's
cold
and
wet
today!");
}
else
if
(temperature
<=
60
&&
precipitation
==
false){
alert("It's
cold
but
dry
today");
}
else
if(precipitation
==
true){
alert("It's
wet,
but
not
cold
out
there");
}
else{
alert("We've
looked
at
the
conditions,
and
it's
neither
cold
nor
wet!");
Since
our
conditions
are
pretty
simple
and
we
only
have
two
on/off
switches,
we
can
logically
omit
the
temperature
check
from
the
third
and
fourth
conditionals.
Review
Syntax
Everything
is
case-sensitive
(e.g.
Javascript
is
not
the
same
as
JavaScript)
As
long
as
the
starting
matches
the
ending,
you
can
use
"double
quotes"
or
'single
quotes'
Parentheses
are
used
for
checking
conditions,
and
sending
or
receiving
a
value
to/from
a
function
Put
reminders
to
a
future
you
by
using
comments.
Use
"//"
for
a
single
line,
or
"/*"
and
"*/"
to
comment
out
large
blocks
of
code
Variables
A
variable
lets
you
set
it
up
once
to
hold
a
value
and
then
allows
you
to
manipulate
its
value
all
throughout
the
script
Variables
can
have
different
data
types
the
common
ones
are:
1. String
(string
of
characters)
2. Number
3. A
true
or
false
value
4. Array
(a
single
object
that
holds
a
number
of
values)
5. Object
(like
an
array,
but
with
named
elements
instead
of
numbered)
Functions
Let
you
make
pieces
of
code
reusable
Make
your
code
more
legible
Need
a
name,
arguments
(variables
to
act
upon),
and
a
block
of
code
that
act
upon
the
inputs
Use
the
"return"
statement
to
send
the
result
back
to
the
place
that
initially
called
the
code
If/Else
if/Else
Compares
values,
and
evaluates
them
to
just
"true"
or
"false"
for
the
sake
of
running
(or
not
running)
a
block
of
code
Can
check
many
values
at
a
time,
using
&&
(AND)
and
||
(OR)
operators
Must
have
an
"if"
statement,
and
can
have
zero
or
more
"else
if"
statements,
and
may
have
one
else
clause
Resources
JavaScript
Books:
Simply
JavaScript
by
Kevin
Yank
&
Cameron
Adams,
SitePoint
JavaScript
Pocket
Reference
by
David
Flanagan,
OReilly
Head
First
JavaScript
by
Michael
Morrison,
OReilly
JavaScript:
Definitive
Guide
by
David
Flanagan,
OReilly
New
to
JavaScript:
For
the
Non-Programmer:
http://webteacher.com/javascript
JavaScript
Primers:
http://www.htmlgoodies.com/primers/jsp/
W3Schools
JavaScript
Tutorial:
http://www.w3schools.com/js/default.asp
Mozilla
Developer
Network
Doc
Center:
A
re-introduction
to
JavaScript:
https://developer.mozilla.org/en/JavaScript/A_re-introduction_to_JavaScript
Online
Resources:
Online
JavaScript
Reference:
https://developer.mozilla.org/en/JavaScript/Guide
About
JavaScript:
https://developer.mozilla.org/en/About_JavaScript
Prototype
Object
Model:
https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model#Class-
Based_vs._Prototype-
Based_Languages
Google
Code
University:
HTML,
CSS
and
JavaScript
from
the
Ground
Up:
http://code.google.com/edu/submissions/html-css-javascript/
About.com
JavaScript
Tutorials:
http://javascript.about.com/od/hintsandtips/Javascript_Tutorials.htm
Webucator
JavaScript
Tutorial:
http://www.learn-javascript-tutorial.com/