| JavaScript - Expressions and Operators | | Print | |
Expressions and Operators
JavaScript expressions are formed by combining values (which may be literals, variables, object properties, array elements, or function invocations) using JavaScript operators. Parentheses can be used in an expression to group subexpressions 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 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) | |
|
9 |
L |
== |
Test for equality |
|
L |
!= |
Test for inequality | |
|
L |
=== |
Test for identity (JS 1.3) | |
|
L |
!== |
Test for non-identity (JS 1.3) | |
|
8 |
L |
& |
Integer bitwise AND |
|
7 |
L |
^ |
Integer bitwise XOR |
|
6 |
L |
| |
Integer bitwise OR |
|
5 |
L |
&& |
Logical AND |
|
4 |
L |
|| |
Logical OR |
|
3 |
R |
?: |
Conditional operator (3 operands) |
|
2 |
R |
= |
Assignment |
|
R |
*=, +=, -=, etc. |
Assignment with operation | |
|
1 |
L |
, |
Multiple evaluation |
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 considers the number 3 and the string "3" to be equal, it considers false to be equal to 0, and it considers null and undefined to be equal. The identity operator, ===, written 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 arguments 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.
- typeof
-
Return the type of the operand as a string. Evaluates to "number", "string", "boolean", "object", "function", or "undefined". Evaluates to "object" if the operand is null.
- instanceof
-
Evaluates to true if the object on the left was created with the constructor function (such as Date or RegExp) on the right.
- in
-
Evaluates to true if the object on the right has (or inherits) a property with the name on the left.
- delete
-
Deletes an object property. Note that this is not the same as simply setting the property to null. Evaluates to false if the property could not be deleted, or true otherwise.
- void
-
Ignores the operand and evaluates to undefined.
| Users' Comments (0) |
|
No comment posted




