Scripting: Grammar

From Waltz
Jump to navigation Jump to search

This documentation is partially reproduced from the excellent Mozilla Developer Network Reference for JavaScript.

JavaScript borrows most of its syntax from Java, but is also influenced by Awk, Perl and Python. JavaScript is case-sensitive and uses the Unicode character set. For example, the word Früh (which means "early" in German) could be used as a variable name.

var Früh = "foobar";

But, the variable früh is not the same as Früh because JavaScript is case sensitive.

In JavaScript, instructions are called statements and are separated by semicolons (;). A semicolon is not necessary after a statement if it is written on its own line. But if more than one statement on a line is desired, then they must be separated by semicolons. ECMAScript also has rules for automatic insertion of semicolons (ASI) to end statements. (For more information, see the detailed reference about JavaScript's lexical grammar.) It is considered best practice, however, to always write a semicolon after a statement, even when it is not strictly needed. This practice reduces the chances of bugs getting into the code.

The source text of JavaScript script gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments, or whitespace. Spaces, tabs, and newline characters are considered whitespace.

Comments

The syntax of comments is the same as in C++ and in many other languages:

// a one line comment
/* this is a longer, 
 * multi-line comment
 */ 
/* You can't, however, /* nest comments */ SyntaxError */

Comments behave like whitespace and are discarded during script execution.

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). Using a dollar sign as the first character in an identifier is discouraged. You can use most of ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers.

Some examples of legal names are Number_hits, temp99, $credit, and _name.

Declaring a Variable

You can declare a variable with the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables, depending on the execution context.

You can also simply assign a value to a variable For example, x = 42. This form creates an undeclared global variable. It also generates a strict JavaScript warning. Undeclared global variables can often lead to unexpected behavior. Thus, it is discouraged to use undeclared global variables.

Evaluating Variables

A variable declared using the var statement with no assigned value specified has the value of undefined. An attempt to access an undeclared variable results in a ReferenceError exception being thrown. You can use undefined to determine whether a variable has a value. In the following code, the variable input is not assigned a value, and the if statement evaluates to true:

var input;
if (input === undefined) {
  doThis();
} else {
  doThat();
}

The undefined value behaves as false when used in a boolean context. For example, the following code executes the function myFunction because the myArray element is undefined:

var myArray = [];
if (!myArray[0]) myFunction();

The undefined value converts to NaN when used in numeric context.

var a;
a + 2;  // Evaluates to NaN

When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:

var n = null;
print(n * 32); // Will log 0 to the console

Scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

In Waltz, the document in a traditional sense is not present. Each snippet of JavaScript you provide is technically a separate document evaluated in its own isolated environment with special access to other nodes ($), as well as the Global object.

Waltz does not guarantee that variables will survive between calls to a script, nor does Waltz guarantee that global variables, both declared or undeclared, will be available on subsequent script executions. This protection is in place to prevent poisoning the scripting environment by redeclaring a key function or object that should not be declarable.

Expressions and Scripting
Concepts Script Expressions · JavaScript
Fundamentals Constructors · Functions · Grammar · Operations · Outputs · Parameters · Referencing Nodes
Advanced Topics Flow Control · Consumer Functions · Loops and Iterations · Reference Chains
Errors Recursion · Script Exceptions · Syntax Errors · Try and Catch