Most operators are used in a statement, or part of a statement, of the form 'left hand side' (lhs) followed by the operator followed by the 'right hand side' (rhs). The lhs and the rhs are variable references.

Operator Category

Operator

Description

Arithmetic Operators

+

(Addition) Adds the numeric value of lhs to the numeric value of rhs unless either the lhs or the rhs is a string (see String Operators).

++

(Increment) Adds one to the numeric value of a variable. If the operator is put before the variable then the increment takes place before the variable is used (pre-increment) and if the operator is put after the variable the increment takes place after the operator is used (post-increment).

-

(Unary negation, subtraction) As a unary operator (preceding a single variable), negates the value of its variable. As a binary operator (between two variables), subtracts the numeric value of rhs from the numeric value of lhs.

--

(Decrement) Subtracts one from the numeric value of a variable. If the operator is put before the variable then the decrement takes place before the variable is used (pre-decrement) and if the operator is put after the variable the decrement takes place after the operator is used (post-decrement).

*

(Multiplication) Multiplies the numeric value of lhs by the numeric value of rhs.

/

(Division) Divides the numeric value of lhs by the numeric value of rhs.

%

(Modulus) Computes the integer remainder of dividing the numeric value of lhs by the numeric value of rhs.

String Operators

+

(String addition) Concatenates the string value of rhs to the string value of lhs.

Note: If either the lhs or the rhs is a String object then the + operator will be interpreted as the string operator.

+=

Concatenates the string value of rhs to the string value of lhs and assigns the result to the lhs.

Logical Operators

&&

(Logical AND) Returns true if both lhs and rhs are true. Otherwise, returns false.

||

(Logical OR) Returns true if either lhs or rhs is true. If both are false, returns false.

!

(Logical negation) If its single operand is true, returns false; otherwise, returns true.

Bitwise Operators

&

(Bitwise AND) Returns a one in each bit position if the bits of both the lhs and the rhs are ones.

^

(Bitwise XOR) Returns a one in a bit position if the bits of either the lhs or the rhs are one but not if the bits of both the lhs and the rhs are one.

|

(Bitwise OR) Returns a one in a bit position if the bits of either the lhs or the rhs are one.

~

(Bitwise NOT) Flips the bits of its operand.

<<

(Left shift) Shifts the lhs in binary representation the number of bits to the left specified in the rhs, shifting in zeros from the right.

>>

(Sign-propagating right shift) Shifts the lhs in binary representation the number of bits to the right specified in the rhs, discarding bits shifted off.

>>>

(Zero-fill right shift) Shifts the lhs in binary representation the number of bits to the right specified in the rhs, discarding bits shifted off, and shifting in zeros from the left.

Assignment Operators

=

Assigns the value of the rhs to the lhs.

+=

Adds the rhs to the lhs and assigns the result to the lhs.

-=

Subtracts the rhs from the lhs and assigns the result to the lhs.

*=

Multiplies the lhs by the rhs and assigns the result to the lhs.

/=

Divides the lhs by the rhs and assigns the result to the lhs.

%=

Divides the lhs by the rhs and assigns the remainder to the lhs.

&=

Performs a bitwise AND between the lhs and the rhs and assigns the result to the lhs.

^=

Performs a bitwise XOR between the lhs and the rhs and assigns the result to the lhs.

|=

Performs a bitwise OR between the lhs and the rhs and assigns the result to the lhs.

<<=

Performs a left shift on the lhs the number of bits specified by the rhs and assigns the result to the lhs.

>>=

Performs a sign-propagating right shift on the lhs the number of bits specified by the rhs and assigns the result to the lhs.

>>>=

Performs a zero-fill right shift on the lhs the number of bits specified by the rhs and assigns the result to the lhs.

Comparison Operators

==

Returns true if the lhs and the rhs are equal.

!=

Returns true if the lhs and the rhs are not equal.

>

Returns true if the lhs is greater than the rhs.

>=

Returns true if the lhs is greater than or equal to the rhs.

<

Returns true if the lhs is less than the rhs.

<=

Returns true if the lhs is less than or equal to the rhs.

Special Operators

.

Used to reference a property held by an object.

[n]

References elements in an Array object, where n starts at 0 for the first element.

[string]

Indirect reference for a property held by an object. The string is evaluated to a name and that name is used as the name of the property. e.g.

 house.table = "dining table";
 var name = "table";
 var myTable1 = house[name];
 var myTable2 = house["table"];
 var myTable3 = house.table;

In this example myTable1, myTable2 and myTable3 all refer to house.table.

new

Lets you create an instance of an object.

 var cars = new Array();
 var myString = new String("Hello");

The following prefixes are special cases: java, com, org, Packages. These prefixes indicate that the object that is being created is a Java object. The prefix "Packages." should be used when a Java object package does not start with one of "java", "com", or "org". The string "Packages." will be removed from the package name before the object is created.

 var javaObject = new com.company.package.JavaObject()
 var javaX = new Packages.mypackage.package.AnotherJavaObject();

var

Specifies that a variable is to be initialized in the current context.

this

Keyword that you can use to refer to the current object.

Comment on this topic

Topic ID: 150081