Versions of JavaScript
The name JavaScript is owned by Netscape. Microsoft's implementation of the language is officially known as JScript, but very few people actually make a distinction between JavaScript and JScript. Versions of JScript are more or less compatible with the equivalent versions of JavaScript, although JScript skipped a version and went directly from JavaScript 1.0 compatibility to JavaScript 1.2 compatibility.
JavaScript has been standardized by ECMA (the organization formerly known as the European Computer Manufacturers Association) and is on the fast track for standardization by the International Standards Organization (ISO). The relevant standards are ECMA-262 and, when standardized by ISO, ISO-16262. These standards define a language officially known as ECMAScript, which is approximately equivalent to JavaScript 1.1, although not all implementations of JavaScript currently conform to all details of the ECMA standard. The name ECMAScript is universally regarded as ugly and cumbersome and was chosen precisely for this reason: it favors neither Netscape's JavaScript nor Microsoft's JScript.
In this chapter, we universally use the term JavaScript to refer to the scripting language. Where certain functionality is implemented only by either Navigator or Internet Explorer, we've noted that fact. When necessary, we use the term ECMA-262 to refer to the standardized version of the language.
The following table specifies what versions of client-side JavaScript are supported by various versions of Netscape Navigator and Microsoft Internet Explorer:
|
Version |
Navigator |
Internet Explorer |
|---|---|---|
|
2 |
JavaScript 1.0 | |
|
3 |
JavaScript 1.1 |
JavaScript 1.0 |
|
4 |
JavaScript 1.2; not fully ECMA-262 compliant prior to Version 4.5 |
JavaScript 1.2;EMCA-262 compliant |
Syntax
JavaScript syntax is modeled on Java syntax; Java syntax, in turn, is modeled on C and C++ syntax. Therefore, C, C++, and Java programmers should find that JavaScript syntax is comfortably familiar.
Case-Sensitivity
JavaScript is a case-sensitive language. All keywords are in lowercase. All variables, function names, and other identifiers must be typed with a consistent capitalization.
Whitespace
JavaScript ignores whitespace between tokens. You may use spaces, tabs, and newlines to format and indent your code in a readable fashion.
Semicolons
JavaScript statements are terminated by semicolons. When a statement is followed by a newline, however, the terminating semicolon may be omitted. Note that this places a restriction on where you may legally break lines in your JavaScript programs: you may not break a statement across two lines if the first line can be a complete legal statement on its own.
Comments
JavaScript supports both C and C++ comments. Any amount of text, on one or more lines, between /* and */ is a comment, and is ignored by JavaScript. Also, any text between // and the end of the current line is a comment, and is ignored. Examples:
// This is a single-line, C++-style comment. /*
* This is a multi-line, C-style comment. * Here is the second line. */
/* Another comment. */ // This too.
Identifiers
Variable, function, and label names are JavaScript identifiers. Identifiers are composed of any number of letters and digits, and _ and $ characters. The first character of an identifier must not be a digit, however. The following are legal identifiers:
i my_variable_name v13 $str
Keywords
The following keywords are part of the JavaScript language, and have special meaning to the JavaScript interpreter. Therefore, they may not be used as identifiers.
|
break |
do |
if |
switch |
typeof |
|
case |
else |
in |
this |
var |
|
catch |
false |
instanceof |
throw |
void |
|
continue |
finally |
new |
true |
while |
|
default |
for |
null |
try |
with |
|
delete |
function |
return |
JavaScript also reserves the following words for possible future extensions. You may not use any of these words as identifiers either.
|
abstract |
enum |
int |
short |
|
boolean |
export |
interface |
static |
|
byte |
extends |
long |
super |
|
char |
final |
native |
synchronized |
|
class |
float |
package |
throws |
|
const |
goto |
private |
transient |
|
debugger |
implements |
protected |
volatile |
|
double |
import |
public |
In addition, you should avoid creating variables that have the same name as global properties and methods: see the Global, Object, and Window reference pages. Within functions, do not use the identifier arguments as an argument name or local variable name.
Variables
Variables are declared and initialized with the var statement:
var i = 1+2+3; var x = 3, message = 'hello world';
Variable declarations in top-level JavaScript code may be omitted, but they are required to declare local variables within the body of a function.
JavaScript variables are untyped: they can contain values of any data type.
Global variables in JavaScript are implemented as properties of a special Global object. Local variables within functions are implemented as properties of the Argument object for that function. Global variables are visible throughout a JavaScript program. Variables declared within a function are only visible within that function. Unlike C, C++, and Java, JavaScript does not have block-level scope: variables declared within the curly braces of a compound statement are not restricted to that block and are visible outside of it.
Data Types
JavaScript supports three primitive data types: numbers, booleans, and strings; and two compound data types: object and arrays. In addition, it defines specialized types of objects that represent functions, regular expressions, and dates.
Numbers
Numbers in JavaScript are represented in 64-bit floating-point format. JavaScript makes no distinction between integers and floating-point numbers. Numeric literals appear in JavaScript programs using the usual syntax: a sequence of digits, with an optional decimal point and an optional exponent. For example:
1 3.14 0001 6.02e23
Integers may also appear in hexadecimal notation. A hexadecimal literal begins with 0x:
0xFF // The number 255 in hexadecimal
When a numeric operation overflows, it returns a special value that represents positive or negative infinity. When an operation underflows, it returns zero. When an operation such as taking the square root of a negative number yields an error or meaningless result, it returns the special value NaN, which represents a value that is not-a-number. Use the global function isNaN( ) to test for this value.
The Number object defines useful numeric constants. The Math object defines various mathematical functions such as Math.sin( ), Math.pow( ), and Math.random( ).
Booleans
The boolean type has two possible values, represented by the JavaScript keywords true and false. These values represent truth or falsehood, on or off, yes or no, or anything else that can be represented with one bit of information.
Strings
A JavaScript string is a sequence of arbitrary letters, digits, and other characters from the 16-bit Unicode character set.
String literals appear in JavaScript programs between single or double quotes. One style of quotes may be nested within the other:
'testing' "3.14" 'name="myform"' "Wouldn't you prefer O'Reilly's book?"
When the backslash character (\) appears within a string literal, it changes, or escapes, the meaning of the character that follows it. The following table lists these special escape sequences:
|
Escape |
Represents |
|---|---|
|
\b |
Backspace |
|
\f |
Form feed |
|
\n |
Newline |
|
\r |
Carriage return |
|
\t |
Tab |
|
\' |
Apostrophe or single quote that does not terminate the string |
|
\" |
Double-quote that does not terminate the string |
|
\ |
Single backslash character |
|
\xdd |
Character with Latin-1 encoding specified by two hexadecimal digits dd |
|
\udddd |
Character with Unicode encoding specified by four hexadecimal digits dddd |
The String class defines many methods that you can use to operate on strings. It also defines the length property, which specifies the number of characters in a string.
The addition (+) operator concatenates strings. The equality (==) operator compares two strings to see if they contain exactly the same sequences of characters. (This is compare-by-value, not compare-by-reference, as C, C++, or Java programmers might expect.) The inequality operator (!=) does the reverse. The relational operators(<, <=, >, and >=) compare strings using alphabetical order.
JavaScript strings are immutable, which means that there is no way to change the contents of a string. Methods that operate on strings typically return a modified copy of the string.