5.1 Expressions
An
expression is a phrase of JavaScript that a
JavaScript interpreter can evaluate to produce a
value. The simplest expressions are
literals
or variable names, like these:
1.7 // A numeric literal
"JavaScript is fun!" // A string literal
true // A boolean literal
null // The literal null value
/java/ // A regular expression literal
{ x:2, y:2 } // An object literal
[2,3,5,7,11,13,17,19] // An array literal
function(x){return x*x;} // A function literal
i // The variable i
sum // The variable sum
The value of a
literal expression is simply
the literal value itself. The value of a variable expression is the
value that the variable contains or refers to.
These expressions are not particularly interesting. More complex (and
interesting) expressions can be created by combining simple
expressions. For example, we saw that 1.7 is an
expression and i is an expression. The following
is also an expression:
i + 1.7
The value of this expression is determined by adding the values of
the two simpler expressions. The + in this example
is an operator that is used to combine two
expressions into a more complex expression. Another operator is
-, which is used to combine expressions by
subtraction. For example:
(i + 1.7) - sum
This expression uses the - operator to subtract
the value of the sum variable from the value of
our previous expression, i + 1.7. JavaScript
supports a number of other operators besides + and
-, as you'll see in the next section.
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.