Expressions et Opérateurs JavaScript

Expressions

Une expression est une suite de literals, variables, opérateurs et expressions qui évoluent vers une seule valeur. Cette valeur peut-être un nombre, une chaîne ou une valeur logique. En fait, il y a deux types d'expressions: Ceux qui assignent une valeur à une variable, et ceux qui ont simplement une valeur. Par exemple l'expression

x = 7

est une expression qui assigne à x la valeur 7. Ce genre d'expressions utilise les opérateurs d'assignement. De l'autre côté, l'expression

3 + 4

équivaut simplement à 7; il n'accompli pas d'assignement. Les opérateurs utilisés dans ce genre d'expressions est simplement nommé opérateur.

JavaScript contient les diferents types d'expression suivants:

Le mot cléfnull défini une valeur nulle. Par contraste, les variables auxqueles on n'a pas définit de variables sont dites indéfinies, et ne peuvent-être utilisées sans une erreur de run-time.

Expressions conditionnelles

Une expression conditionnelle peut avoir une ou deux valeures basées sur une condition. La syntaxe est:

(condition) ? val1 : val2

Si condition est vérifiée, l'expression à la valeur val1, sinon elle a la valeur val2. Vous pouvez utiliser une expression conditionnelle partout ou vous pourriez utiliser une expression standard.

Par exemple,

status = (age >= 18) ? "adulte" : "mineur"
Cette déclaration assigne la valeur "adulte" à une variable si age est de 18 ou plus. sinon, elle lui assigne la valeur "mineur".

Opérateurs d'assignement (=, +=, -=, *=, /=)

Un opérateur d'assignement assigne une valeur à son opérande de gauche basée sur la avleur de l'opérande de droite. L'opérateur d'assignement basique est égal (=), qui assigne la valeur de l'opérande de droite à l'opérande de gauche.

Les autres opérateurs sont des raccourcis pour des équations arithmetiques toutes simples:

There are additional assignment operators for bitwise operations:

Operators

LiveScript has arithmetic, string, and logical operators. 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.

Arithmetic Operators

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.

Standard Arithmetic Operators

The standard arthmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work in the standard way.

Modulus (%)

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 statement above, where var1 and var2 are variables. The modulo function is the remainder of integrally dividing var1 by var2. For example, 12 % 5 returns 2.

Increment (++)

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 3, then the statement

y = x++

increments x to 4 and sets y to 3.

If x is 3, then the statement

y = ++x

increments x to 4 and sets y to 4.

Decrement (--)

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 3, then the statement

y = x--

decrements x to 2 and sets y to 3.

If x is 3, then the statement

y = --x

decrements x to 2 and sets y to 2.

Unary negation (-)

The unary negation operator must precede its operand. It negates its operand. For example,

x = -x

negates the value of x; that is if x were 3, it would become -3.

Les opérateurs Bitwise

Les opérateurs Bitwise traitent leurs opérandes en tant que bits (zeéros et un), plutôt que comme des décimaux, héxadécimaux, ou octaux. Par exemple, le nombre décimal 9 a une representation binaire de 101. Les opérateurs bitwise accomplissent leurs opérations sur de telles representations binaires, mais ils renvoient des valeures numériques standard de JavaScript.

opérateurs logiques Bitwise

Les operateurs Bitwise logiques sont:

The bitwise logical operators work conceptually as follows:

Bitwise Shift Operators

The bitwise shift operators are:

The 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 32-bit integers, and return a result of the same type as the left operator.

Left Shift (<<)

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.

Example TBD.

Sign-propagating Right Shift (>>)

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.

Example TBD.

Zero-fill right shift (>>>)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.

Example TBD.

Logical Operators

Logical operators take logical (Boolean) values as operands. They return a logical value. Logical values are true and false.

And (&&)

Usage: expr1 && expr2

The logical "and" operator returns true if both logical expressions expr1 and expr2 are true. Otherwise, it returns false.

Or (||)

Usage: expr1 || expr2

The logical "or" operator returns true if either logical expression expr1 or expr2 is true. If both expr1 and expr2 are false, then it returns false.

Not (!)

Usage: !expr

The logical "not" operator is a unary operator that negates its operand expression expr. That is, if expr is true, it returns false, and if expr is false, then it returns true.

Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short circuit" evaluation using the following rule:

The rules of logic guarantee that these evaluations will always be correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.

Comparison Operators (= =, >, >=, <, <=, !=)

A comparison operator compares its operands and returns a logical value based on whether the comparison is true or not. The operands may be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical ordering.

The operators are:

String Operators

In addition to the comparison operators, which may 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 is a string that has the value "alpha", then the expression

mystring += "bet"
evaluates to "alphabet" and assigns this value to mystring.

Operator Precedence

The precedence of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.

The precedence of operators, from lowest to highest is as follows:

comma ,
assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
conditional ?:
logical-or ||
logical-and &&
bitwise-or |
bitwise-xor ^
bitwise-and &
equality == !=
relational < <= > >=
shift << >> >>>
addition/subtraction + -
multiply/divide * / %
negation/increment ! ~ - ++ --
call, member () [] .