Programs consist of one or more statements that operate on data values. The latter are expressed either as constants or variables. Statements are divided between expressions, which compute a value using operators of various sorts (e.g., the arithmetic operators +, -, * and =); and commands, which change the state of the program by assigning values to variables, or by performing some side-effecting operation (e.g. “print”).
The values used in a program are categorized according to their datatype. Virtually all programming languages include one or more numerical types (e.g., integer and reals, also called floating point). Another common datatype consists of textual data, which are called character strings, or just strings. Finally, languages generally provide some means of combining multiple values into a single entity. These include arrays, which are sequences of values indexed by integers; and structures or objects, in which the constiuents, or fields, are labeled by a string property names.
Variables are declared using the var keyword:
var x = 17, y = "hello";
var y;
In the latter case y
is initialize to undefined
. Variables are not restricted by datatype and can be assigned
a value of any JavaScript type.
1, 3.14, 2.78e01
"This is a character string"
, "So is this"
.var a = new Array();
Array constants are denoted using square brackets; e.g., var b = [1,2,3,4].
Similarly, array components use square brackets to denote the index for assignment and access; e.g., b[0], b[1]
. Unlike most languages, however, the index set may contain holes; e.g., b[0], b[1]
, and b[7]
may be defined while b[2]
through b[6]
are not.var c = new Object();
Object constants are denoted using curly brackets enclosing the name-value pairs, and may included embedded object constants as values:
var a_person = {name: "Steve", date_of_birth: {month: 1, day: 28, year: 1980} }
Functions are defined using one of two similar syntaxes:
var double = function(x){return 2 * x;}
function triple(y){return double(y) + y;}
The first case allows you to create a function without the necessity of providing a name. For example, if
var apply_to_two = function(f){return f(2);}
Then invoking apply_to_two(function(z){return z * z;})
will result in 4
. This also shows
that functions may be passed as parameters to other functions.
The index variablefor (var i in a) { print(a[i]); }
i
will cycle through all defined indices in a
. Another important feature involves the conditional, or “if” statement. Suppose we want to assign the maximum of x
and y
to z.
One way would be
if (x > y) { z = x; } else { z = y; }
This familiar statement is called a conditional command because it provides a pair of alternative commands, only 1 of which is actually executed, depending on the outcome of the conditional test.
An alternate way of doing the same thing uses the conditional expression:
z = (x > y) ? x : y;
The right hand side expression produces one of a pair of values again depending on the outcome of the conditional test. This form is particularly useful in creating the Nova expressions that appear in component definitions.
Actual computation (i.e., combining and manipulating data to create new values) is performed by
JavaScript's primitive operators, or primops. The most familiar primops to most people are the standard arithmetic operators (+, -,* , =). JavaScript provides additional mathematical operators via the Math object. This is a special object whose properties are bound to various mathematical functions. For example, Math.sin(x)
computes the trigonometric sine of the value of x
. A reference to the complete listing of available Math primops is provided here.
A second large set of primops is provided by the underscore.js library. These functions are all properties of a special object denoted by the underscore character, _, and are documented on the library's Web page. For example, _.last(a)
will return the last element of the array a
.