his chapter describes JavaScript expressions and operators, including assignment,
comparison, arithmetic, bitwise, logical, string, and special operators.
An expression is any valid set of literals, variables,
operators, and expressions that evaluates to a single value; the value can
be a number, a string, or a logical value.
Conceptually, there are two types of expressions: those
that assign a value to a variable, and those that simply have a value. For
example, the expression
x = 7
is an expression that assigns x the value seven. This
expression itself evaluates to seven. Such expressions use assignment
operators. On the other hand, the expression 3 + 4
simply
evaluates to seven; it does not perform an assignment. The operators used
in such expressions are referred to simply as operators.
JavaScript has the following types of expressions:
-
-
Arithmetic: evaluates to a number, for example 3.14159
-
String: evaluates to a character string, for example, "Fred" or "234"
-
Logical: evaluates to true or false
The special keyword null denotes a null value.
In contrast, variables that have not been assigned a value are
undefined and will cause a runtime error if used as numbers or as
numeric variables. Array elements that have not been assigned a value, however,
evaluate to false. For example, the following code executes the function
myFunction because the array element is not defined:
myArray=new Array()
if (!myArray["notThere"])
myFunction()
A conditional expression can have one of two values based
on a condition. The syntax is
(condition) ? val1 : val2
If condition is true, the expression has the value
of val1. Otherwise it has the value of val2. You can use a
conditional expression anywhere you would use a standard expression.
For example,
status = (age >= 18) ? "adult" : "minor"
This statement assigns the value "adult" to the variable
status if age is eighteen or greater. Otherwise, it assigns the value
"minor" to status.
JavaScript has assignment, comparison, arithmetic, bitwise,
logical, string, and special operators. This section describes the operators
and contains information about operator precedence.
There are both binary and
unary operators. A binary operator requires two operands, one before
the operator and one after the operator:
operand1 operator operand2
For example, 3+4
or x*y
.
A unary operator requires a single operand, either before
or after the operator:
operator operand
or
operand operator
For example, x++
or ++x
.
Implemented in Navigator 2.0
An assignment operator assigns a value to its left operand
based on the value of its right operand. The basic assignment operator is
equal (=), which assigns the value of its right operand to its left operand.
That is, x = y assigns the value of y to x.
The other operators are shorthand
for standard operations, as shown in the following table:
Implemented in Navigator 2.0
A comparison operator compares its operands and returns
a logical value based on whether the comparison is true or not. The operands
can be numerical or string values. When used on string values, the comparisons
are based on the standard lexicographical ordering. They are described in
the following table.
Arithmetic operators take numerical values (either literals
or variables) as their operands and return a single numerical value. The
standard arithmetic operators are addition (+), subtraction (-), multiplication
(*), and division (/). These operators work as they do in other programming
languages.
Implemented in Navigator 2.0
The modulus operator is used as follows:
var1 % var2
The modulus operator returns the first operand modulo
the second operand, that is, var1 modulo var2, in the preceding
statement, where var1 and var2 are variables. The modulo function
is the floating-point remainder of dividing var1 by var2. For
example, 12 % 5 returns 2.
Implemented in Navigator 2.0
The increment operator is used as follows:
var++
or
++var
This operator increments (adds one to) its operand and
returns a value. If used postfix, with operator after operand (for example,
x++), then it returns the value before incrementing. If used prefix with
operator before operand (for example, ++x), then it returns the value after
incrementing.
For example, if x is three, then
the statement y = x++
sets y to three and
increments
x to four. If x is three, then the statement y = ++x
increments
x to four and sets y to four.
Implemented in Navigator 2.0
The decrement operator is used as follows:
var--
or
--var
This operator decrements (subtracts one from) its operand
and returns a value. If used postfix (for example, x--), then it returns
the value before decrementing. If used prefix (for example, --x), then it
returns the value after decrementing.
For example, if x is three, then
the statement y = x--
sets y to three and decrements x to two.
If x is three, then the statement y = --x
decrements x to two
and sets y to two.
Implemented in Navigator 2.0
The unary negation precedes its operand and negates it.
For example, x = -x
negates the value of x; that is, if x were
three, it would become -3.
Bitwise operators treat their operands as a set of bits
(zeros and ones), rather than as decimal, hexadecimal, or octal numbers.
For example, the decimal number nine has a binary representation of 1001.
Bitwise operators perform their operations on such binary representations,
but they return standard JavaScript numerical values.
The following table summarizes
JavaScript's bitwise operators
Implemented in Navigator 2.0
The bitwise logical operators work conceptually as
follows:
-
-
The operands are converted to thirty-two-bit integers and expressed by a
series of bits (zeros and ones).
-
Each bit in the first operand is paired with the corresponding bit in the
second operand: first bit to first bit, second bit to second bit, and so
on.
-
The operator is applied to each pair of bits, and the result is constructed
bitwise.
For example, the binary representation of nine is 1001,
and the binary representation of fifteen is 1111. So, when the bitwise operators
are applied to these values, the results are as follows:
-
-
15 & 9 yields 9 (1111 & 1001 = 1001)
-
15 | 9 yields 15 (1111 | 1001 = 1111)
-
15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
Implemented in Navigator 2.0
The bitwise shift operators take two operands: the first
is a quantity to be shifted, and the second specifies the number of bit positions
by which the first operand is to be shifted. The direction of the shift operation
is controlled by the operator used.
Shift operators convert their operands
to thirty-two-bit integers and return a result of the same type as the left
operator.
This operator shifts the first operand the specified number
of bits to the left. Excess bits shifted off to the left are discarded. Zero
bits are shifted in from the right.
For example, 9<<2 yields
thirty-six, because 1001 shifted two bits to the left becomes 100100, which
is thirty-six.
This operator shifts the first operand the specified number
of bits to the right. Excess bits shifted off to the right are discarded.
Copies of the leftmost bit are shifted in from the left.
For example, 9>>2 yields
two, because 1001 shifted two bits to the right becomes 10, which is two.
Likewise, -9>>2 yields -3, because the sign is preserved.
This operator shifts the first operand the specified number
of bits to the right. Excess bits shifted off to the right are discarded.
Zero bits are shifted in from the left.
For example, 19>>>2 yields
four, because 10011 shifted two bits to the right becomes 100, which is four.
For non-negative numbers, zero-fill right shift and sign-propagating right
shift yield the same result.
Implemented in Navigator 2.0
Logical operators take Boolean (logical) values as operands
and return a Boolean value. They are described in the following table.
As logical expressions are evaluated left to right, they
are tested for possible "short-circuit" evaluation using the following
rules:
-
-
false && anything is short-circuit evaluated to false.
-
true || anything is short-circuit evaluated to true.
The rules of logic guarantee that these evaluations are
always correct. Note that the anything part of the above expressions
is not evaluated, so any side effects of doing so do not take effect.
Implemented in Navigator 2.0
In addition to the comparison operators, which can be
used on string values, the concatenation operator (+) concatenates two string
values together, returning another string that is the union of the two operand
strings. For example, "my " + "string"
returns the string
"my string".
The shorthand assignment operator += can also be used
to concatenate strings. For example, if the variable mystring has
the value "alpha," then the expression mystring += "bet"
evaluates
to "alphabet" and assigns this value to mystring.
Implemented in Navigator 2.0
You can use the new operator to create an instance
of a user-defined object type or of one of the built-in object types Array,
Boolean, Date, Function, Math, Number, or String. Use new
as follows:
objectName = new objectType ( param1 [,param2] ...[,paramN] )
For more information, see
"new".
Implemented in Navigator 3.0
The typeof operator is used in either of the following
ways:
1. typeof operand
2. typeof (operand)
The typeof operator returns a string indicating the type
of the unevaluated operand. operand is the string, variable, keyword,
or object for which the type is to be returned. The parentheses are
optional.
Suppose you define the following variables:
var myFun = new Function("5+2")
var shape="round"
var size=1
var today=new Date()
The typeof operator returns the following results for
these variables:
typeof myFun is object
typeof shape is string
typeof size is number
typeof today is object
typeof dontExist is undefined
For the keywords true and null, the typeof operator returns
the following results:
typeof true is boolean
typeof null is object
For a number or string, the typeof operator returns the
following results:
typeof 62 is number
typeof 'Hello world' is string
For property values, the typeof operator returns the type
of value the property contains:
typeof document.lastModified is string
typeof window.length is number
typeof Math.LN2 is number
For methods and functions, the typeof operator returns
results as follows:
typeof blur is function
typeof eval is function
typeof parseInt is function
typeof shape.split is function
For objects, the typeof operator returns results as
follows:
typeof Date is function
typeof Function is function
typeof Math is function
typeof Option is function
typeof String is function
Implemented in Navigator 3.0
The void operator is used in either of the following
ways:
1. javascript:void (expression)
2. javascript:void expression
The void operator specifies an expression to be evaluated
without returning a value. expression is a JavaScript expression to
evaluate. The parentheses surrounding the expression are optional, but it
is good style to use them.
You can use the void operator to specify an expression
as a hypertext link. The expression is evaluated but is not loaded in place
of the current document.
The following code creates a hypertext
link that does nothing when the user clicks it. When the user clicks the
link, void(0)
evaluates to 0
, but that has no effect
in JavaScript.
<A HREF="javascript:void(0)">Click here to do nothing</A>
The following code creates a hypertext link that submits
a form when the user clicks it.
<A HREF="javascript:void(document.form.submit())">Click here to submit</A>
For information on creating hypertext links, see
"Area" and "Link
object".
The precedence of operators determines the order
they are applied when evaluating an expression. You can override operator
precedence by using parentheses.
The following table describes the precedence of operators,
from lowest to highest:
file: /Techref/language/java/SCRIPT/expr.htm, 31KB, , updated: 2009/2/2 13:27, local time: 2024/12/11 19:36,
|
| ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/TECHREF/language/java/SCRIPT/expr.htm"> Expressions and operators </A> |
Did you find what you needed?
|
.