Javascript Light

NovaScript and JavaScript

NovaScript is an extension of a well-known and widely used language called JavaScript. This means that NovaScript uses JavaScript syntax for all of its code; moreover, any legal JavaScript program is also a NovaScript program. This includes the code used for initial Stock values, Flows and Terms. Fortunately, there are many good sources for learning to program in JavaScript. We will review a few important ideas about programming as it applies to building Nova models below.

Statements, Commands, Expressions

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.

JavaScript Highlights

JavaScript has become a prominent language if for no other reason than its role as the standard for programming the behavior of Web browsers. Here is a brief list of JavaScript highlights which will be of particular use in NovaScript. While this review is not enough to teach you how to program, it will help you to understand much of the JavaScript that appears in Nova. Click here for a complete JavaScript reference.

Variables

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.

Datatypes

Program structures

JavaScript adapts the structure of the C and C++ languages for its code. Common control structures (i.e. coding structures that direct the flow of the program) include conditionals, for-loops and while loops. One particularly useful version of the for-loop specific to JavaScript has the following form, assuming variable a refers to either an array or object:
for (var i in a) {
    print(a[i]);
}
The index variable 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.

Primitive operators

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.