The JavaScript Language
|
9
the keyword this refers to the object for which the function
is a property.
Within the body of a function, the
arguments[] array con-
tains the complete set of arguments passed to the function.
See Function and Arguments in the reference section.
null and undefined
There are two JavaScript values that are not of any of the
types described above. The JavaScript keyword
null is a spe-
cial value that indicates “no value”. If a variable contains
null, you know that it does not contain a valid value of any
other type. The other special value in JavaScript is the unde-
fined value. This is the value of uninitialized variables and
the value returned when you query object properties that do
not exist. In JavaScript 1.5, there is a pre-defined global vari-
able named
undefined that holds this special undefined
value.
null and undefined serve similar purposes and the ==
operator considers them equal; if you need to distinguish
between them, use the
=== operator.
Expressions and Operators
JavaScript expressions are formed by combining values
(which may be literals, variables, object properties, array ele-
ments, or function invocations) using JavaScript operators.
Parentheses can be used in an expression to group subex-
pressions and alter the default order of evaluation of the
expression. Some examples:
1+2
total/n
sum(o.x, a[3])++
JavaScript defines a complete set of operators, most of which
should be familiar to C, C++, and Java programmers. They
are listed in the table below, and a brief explanation of the
non-standard operators follows. The P column specifies
operator precedence and the A column specifies operator
10
|
JavaScript Pocket Reference
associativity: L means left-to-right associativity, and R means
right-to-left associativity.
P A Operator Operation performed
15 L . Access an object property
L
[] Access an array element
L
() Invoke a function
R
new Create new object
14 R
++ Pre-or-post increment (unary)
R
-- Pre-or-post decrement (unary)
R
- Unary minus (negation)
R
+ Unary plus (no-op)
R
~ Bitwise complement (unary)
R
! Logical complement (unary)
R
delete Undefine a property (unary) (JS 1.2)
R
typeof Return data type (unary) (JS 1.1)
R
void Return undefined value (unary) (JS 1.1)
13 L
*, /, % Multiplication, division, remainder
12 L
+, - Add, subtract
L
+ Concatenate strings
11 L
<< Integer shift left
L
>> Shift right, sign-extension
L
>>> Shift right, zero extension
10 L
<, <= Less than, less than or equal
L
>, >= Greater than, greater than or equal
L
instanceof Check object type (JS 1.5)
L
in Check whether property exists (JS 1.5)
9L
== Test for equality
L
!= Test for inequality
L
=== Test for identity (JS 1.3)
L
!== Test for non-identity (JS 1.3)
The JavaScript Language
|
11
JavaScript operators that are not familiar from C, C++, and
Java are the following:
=== and !==
The JavaScript equality operator, ==, defines equality
loosely and allows type conversions. For example, it con-
siders the number
3 and the string ”3“ to be equal, it con-
siders
false to be equal to 0, and it considers null and
undefined to be equal. The identity operator, ===, writ-
ten with three equals signs, is stricter: it only evaluates to
true if its operands are identical: i.e. if they have the
same type and are equal. Similarly, the JavaScript non-
identity operator
!== is stricter than the non-equality !=
operator.
String operators
In JavaScript, the
+ operator concatenates string argu-
ments in addition to adding numeric arguments. The
==
and === operators compare strings by value by testing to
see whether they contain exactly the same characters.
The relational operators
<, <=, >, and >= compare strings
based on alphabetical order.
8L& Integer bitwise AND
7L
^ Integer bitwise XOR
6L
| Integer bitwise OR
5L
&& Logical AND
4L
|| Logical OR
3R
?: Conditional operator (3 operands)
2R
= Assignment
R
*=, +=, -=, etc. Assignment with operation
1 L , Multiple evaluation
P A Operator Operation performed

Get JavaScript Pocket Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.