This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. Notice that there is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in Navigator. However, the date object and related built-in functions enable you to handle dates.
Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
var answer = 42
And later, you could assign the same variable a string value, for example:
answer = "Thanks for all the fish..."
x = "The answer is " + 42 y = 42 + " is the answer."
The first statement will the string "The answer is 42". The second statement will return the string "42 is the answer".
You use variables to hold values in your application. You give these variables names by which you reference them, and there are certain rules to which the names must conform.
Some examples of legal names are:
To declare a local variable inside a function, use the var keyword, for example:
var total = 0
To declare a global variable, declare the variable by assignment, that is simply assign the desired value to the variable (either in a function or outside a function), for example:
total = 0
It is good programming practice to declare global variables at the beginning of your script, so that functions will inherit the variable and its value.
"To be or not to be"
Integers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) format. A decimal integer literal consists of a sequence of digits (optionally suffixed as described below) without a leading 0 (zero).
An integer can be expressed in octal or hexadecimal rather than decimal. A leading 0 (zero) on an integer literal means it is in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.
A floating point literal can have the following parts: a decimal integer, a decimal point ("."), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by a "+" or "-"). A floating point literal must have at least one digit, plus either a decimal point or "e" (or "E"). Some examples of floating point literals are:
The boolean type has two literal values: true and false.
A string literal is zero or more characters enclosed in double (") or single (') quotes. A string must be delimited by quotes of the same type; that is, either both single quotes or double quotes. The following are examples of string literals:
"one line \n another line"
You can insert quotes inside of strings by preceding them by a backslash. This is known as escaping the quotes. For example,
var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service" document.write(quote)
The result of this would be