JavaScript Editor JavaScript Validator     JavaScript Editor 



Team LiB
Previous Section Next Section

JavaScript Operators

Assignment operators were introduced in JavaScript 1.0 and Jscript 1.0.

Name

Introduced

Meaning

Assignment

JavaScript 1.0 Jscript 1.0

Sets variable v1 to the value of variable v2

Shorthand Addition
or
Shorthand Concatenation

JavaScript 1.0 Jscript 1.0

v1 = v1 + v2

Shorthand Subtraction

JavaScript 1.0 Jscript 1.0

v1 = v1 - v2

Shorthand Multiplication

JavaScript 1.0 Jscript 1.0

v1 = v1 * v2

Shorthand Division

JavaScript 1.0 Jscript 1.0

v1 = v1 / v2

Shorthand Modulus

JavaScript 1.0 Jscript 1.0

v1 = v1 % v2

Shorthand left shift

JavaScript 1.0 Jscript 1.0

v1 = v1 << v2

Shorthand right shift

JavaScript 1.0 Jscript 1.0

v1 = v1 >> v2

Shorthand zero fill right shift

JavaScript 1.0 Jscript 1.0

v1 = v1 >>> v2

Shorthand AND

JavaScript 1.0 Jscript 1.0

v1 = v1 & v2

Shorthand XOR

JavaScript 1.0 Jscript 1.0

v1 = v1 ^ v2

Shorthand OR

JavaScript 1.0 Jscript 1.0

v1 = v1 | v2

Comparison operators were first introduced in JavaScript 1.0, though a few changes have been made as noted. If v1=1 and v2=2, the following example statements are all true. Boolean literal values true and false are returned by the comparisons.

In JavaScript 1.4, == has been deprecated with respect to objects in favor of JSObject.equals. In JavaScript 1.2 only, neither == nor !== attempted type conversion before returning a value.

Name

Introduced

Meaning

Equal

JavaScript 1.0 Jscript 1.0

True if two operands are strictly equal or equal once cast to the same type.

Not Equal

JavaScript 1.0 Jscript 1.0

True if two operands are not strictly equal or not equal once cast to the same type.

Greater Than

JavaScript 1.0 Jscript 1.0

True if LHS operand is greater than RHS operand.

Greater Than Or Equal

JavaScript 1.0 Jscript 1.0

True if LHS operand is greater than or equal to RHS operand.

Less Than

JavaScript 1.0 Jscript 1.0

True if LHS operand is less than RHS operand.

Less Than Or Equal

JavaScript 1.0 Jscript 1.0

True if LHS operand is less than or equal to RHS operand.

Strictly Equal

JavaScript 1.3 Jscript 1.0

Not in ECMA-262. True if operands are equal and of same type.

Not Strictly Equal

JavaScript 1.3 Jscript 1.0

Not in ECMA-262. True if operands are not strictly equal.

Arithmetic operators were first introduced in JavaScript 1.0 and Jscript 1.0.

Name

Introduced

Result

Addition

JavaScript 1.0 Jscript 1.0

Sum of v1 and v2 concatenation of v1 and v2, if they are strings.

Subtraction

JavaScript 1.0 Jscript 1.0

Difference of v1 and v2.

Multiplication

JavaScript 1.0 Jscript 1.0

Product of v1 and v2.

Division

JavaScript 1.0 Jscript 1.0

Quotient of v2 into v1.

Modulus

JavaScript 1.0 Jscript 1.0

Integer remainder of

Prefix Increment

JavaScript 1.0 Jscript 1.0

(v1 + 1) * v2. Note: v1 will be left as v1 + 1.

Postfix Increment

JavaScript 1.0 Jscript 1.0

(v1 * v2); v1 is then incremented by 1.

Prefix Decrement

JavaScript 1.0 Jscript 1.0

(v1 – 1) * v2. Note: v1 is left as v1 - 1.

Postfix Decrement

JavaScript 1.0 Jscript 1.0

(v1 * v2). v1 is then decremented by 1.

Bitwise logical operators were first introduced in JavaScript 1.0 and Jscript 1.0. They work by converting values in v1 and v2 to 32-bit binary numbers and then comparing the individual bits of these two binary numbers. The result is returned as a normal decimal number.

Name

Introduced

Result

Bitwise AND

JavaScript 1.0 Jscript 1.0

The bitwise AND is true only when both bits are set.

Bitwise OR

JavaScript 1.0 Jscript 1.0

The bitwise OR is true if either bits are set.

Bitwise XOR

JavaScript 1.0 Jscript 1.0

The Bitwise XOR returns a one in each bit position for which the corresponding bits of either but not both operands are ones.

Bitwise NOT

JavaScript 1.0 Jscript 1.0

Inverts all the bits in the number.

Bitwise shift operators were first introduced in JavaScript 1.0 and Jscript 1.0. They work by converting values in v1 to 32-bit binary numbers and then moving the bits in the number to the left or the right by the specified number of places.

Name

Introduced

Result

Left Shift

JavaScript 1.0 Jscript 1.0

Shifts v1 to the left by v2 places, filling the new gaps in with zeros.

Right Shift

JavaScript 1.0 Jscript 1.0

Shifts v1 to the right by v2 places, ignoring the bits shifted off the number.

Sign Propagating Right Shift

JavaScript 1.4 Jscript 1.0

Shifts v1 to the right by v2 places, ignoring the bits shifted off the number.

Zero-fill Right Shift

JavaScript 1.0 Jscript 1.0

Shifts v1 to the right by v2  places, ignoring the bits shifted off the number and adding v2 zeros to the left of the number.

Logical operators were first introduced in JavaScript 1.0 and Jscript 1.0. They should return one of the Boolean literals, true or false. However, this may not happen if either v1 or v2 is neither a Boolean value nor a value that easily converts to a Boolean value, such as 0, 1, null, the empty string, or undefined.

Name

Introduced

Result

Logical AND

JavaScript 1.0 Jscript 1.0

Returns true if both v1 and v2 are true, false otherwise. Will not evaluate v2 if v1 is false.

Logical OR

JavaScript 1.0 Jscript 1.0

Returns false if both v1 and v2 are false, true otherwise. Will not evaluate v2 if v1 is true.

Logical NOT

JavaScript 1.0 Jscript 1.0

Returns false if v1 is true, true otherwise.

Several operators have been introduced to JavaScript to deal with objects.

Name

Introduced

Description

delete

JavaScript 1.2 Jscript 3.0

Deletes an object, one of its properties or the element of an array at the specified index.

in

JavaScript 1.4

Not in ECMA-262. Returns true if someObject has the named property. Not supported by Internet Explorer.

instanceof

JavaScript 1.4 Jscript 5.0

Not in ECMA-262. Returns true if someObject is of type objectType.

new

JavaScript 1.0 Jscript 1.0

Creates a new instance of an object with type objectType. The parameterList obeys a constructor function specified elsewhere.

this

JavaScript 1.0 Jscript 1.0

Refers to the current object.

JavaScript has several other miscellaneous operators.

Name

Introduced

Description

Conditional Operator

JavaScript 1.0 Jscript 1.0

If evalquery is true, the operator returns v1, else it returns v2.

Comma Operator

JavaScript 1.0 Jscript 1.0

Evaluates both eval1 and eval2 while treating the two as one expression.

typeof

JavaScript 1.1 Jscript 1.0

Returns a string holding the type of v1, which is not evaluated.

void

JavaScript 1.1 Jscript 2.0

Evaluates eval1 but does not return a value.

Operator Precedence

Does 1 + 2 * 3 = 1 + (2 * 3) = 7 or does it equal (1 + 2) * 3 = 9?

The ECMAScript standard doesn't yet clearly document operator precedence. However, JavaScript closely follows Java, and Java closely follows C. The following table shows precedence with the highest at the top, and like operators grouped together. The third column explains whether to read 1+2+3+4 as ((1+2)+3)+4 or 1+(2+(3+(4))).

Operator Type

Operators

Evaluation Order for Like Elements

comma

,

left to right

Conditional

? :

left to right

postfix operators

[] () . expr++ expr—

left to right

unary operators

++expr —expr +expr -expr ~ !

right to left

object management or cast

delete new typeof void

right to left

multiplicative

* / %

left to right

additive/subtractive

+ -

left to right

negation/increment

! ~ - + ++ — typeof void delete

left to right

Call

()

left to right

shift

<< >> >>>

left to right

relational

< > <= >= in instanceof

left to right

equality

== != === !==

left to right

bitwise AND

&

left to right

bitwise exclusive OR

^

left to right

Bitwise shift

<< >> >>>

left to right

bitwise OR

|

left to right

logical AND

&&

left to right

logical OR

||

left to right

conditional

? :

right to left

assignment

= += -= &= <<= >>== &= ^= |=

right to left

multiple evaluation

,

left to right

create instance

new

 

Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Validator     JavaScript Editor


©