Javascript offers built-in operators in the following categories:
Assignment operators
Arithmetic operators
String operators
Logical operators
Comparison operators
Bit-wise operators
Special operators
These operators are used to assign the value of expressions to variables.
var variable = expression;
The assignment operator is used to assign the value of an expression to the variable.
It is an error to attempt to assign to a constant.
variable += expression;
This operator adds the value of the expression to the variable. It is the same as:
variable = variable + expression;
but is shorter to write and less error-prone.
See also += string operator.
variable -= expression;
This operator subtracts the value of the expression from the variable.
variable *= expression;
This operator multiplies the value of the expression by the value of the variable.
variable /= expression;
This operator divides the value of the variable by the value of the expression.
variable %= expression;
This operator divides the variable by the expression and assigns the remainder of the division (which may be 0), to the variable.
variable &= expression;
This operator performs a bit-wise AND on the value of the expression and the value of the variable and assigns the result to the variable.
variable ^= expression;
This operator performs a bit-wise OR on the value of the expression and the value of the variable and assigns the result to the variable.
variable |= expression;
This operator performs a bit-wise OR on the value of the expression and the value of the variable and assigns the result to the variable.
variable <<= expression;
This operator performs a bit-wise left shift on the variable by an expression number of bits. Zeros are shifted in from the right.
variable >>= expression;
This operator performs a bit-wise (sign-preserving) right shift on the variable by an expression number of bits.
variable >>>= expression;
This operator performs a bit-wise (zero-padding) right shift on the variable by an expression number of bits.
These operators are used to perform arithmetic computations on their operands.
operand1 + operand2
This operator returns the result of adding the two operands (operand1 and operand2).
See also + string operator.
++operand; // pre-increment operand++; // post-increment
The pre-increment version of this operator increments the operand and returns the value of the (now incremented) operand.
The post-incremented version of this operator returns the value of the operand and then increments the operand.
var result = operand1 - operand2; // subtraction operand = -operand; // unary negation
The subtraction version of this operator returns the result of subtracting its second operand (operand2) from its first operand (operand1).
The unary negation version of this operator returns the result of negating (changing the sign) of its operand.
--operand; // pre-decrement operand--; // post-decrement
The pre-decrement version of this operator decrements the operand and returns the value of the (now decremented) operand.
The post-decremented version of this operator returns the value of the operand and then decrements the operand.
operand1 * operand2
This operator returns the result of multiplying the two operands (operand1 and operand2).
operand1 / operand2
This operator returns the result of dividing the first operand (operand1) by the second operand (operand2).
Note that division by zero is not an error. The result of division by zero is Infinity.
operand1 % operand2
This operator returns the integer remainder (which may be 0) from the division of operand1 by operand2.
These operators provide string functions using operators. Many other string functions are available, see String.
str1 + str2
This operator returns a string that is the concatenation of its operands, (str1 and str2).
See also + arithmetic operator.
str1 += str2
This operator appends its second operand (str2) onto the end of the first operand (str1).
See also += assignment operator.
These operators are used to evaluate whether their operands are true or false. The binary operators use short-circuit logic, that is, they do not evaluate their second operand if the logical value of the expression can be determined by evaluating the first operand alone.
operand1 && operand2
This operator returns an object whose value is true if both its operands are true; otherwise it returns an object whose value is false.
Specifically, if the value of operand1 is false, the operator returns operand1 as its result. If operand1 is true, the operator returns operand2.
operand1 || operand2
This operator returns an object whose value is true if either of its operands are true; otherwise it returns an object whose value is false.
Specifically, if the value of operand1 is true, the operator returns operand1 as its result. If operand1 is false, the operator returns operand2.
! operand
If the operand's value is true, this operator returns false; otherwise it returns true.
These operators are used to compare objects and their values.
operand1 == operand2
Returns true if the operands are equal; otherwise returns false.
operand1 != operand2
Returns true if the operands are not equal; otherwise returns false.
operand1 === operand2
Returns true if the operands are equal and of the same type; otherwise returns false.
operand1 !== operand2
Returns true if the operands are not equal or if the operands are of different types;otherwise returns false.
operand1 > operand2
Returns true if operand1 is greater than operand2; otherwise returns false.
operand1 >= operand2
Returns true if operand1 is greater than or equal to operand2; otherwise returns false.
operand1 < operand2
Returns true if operand1 is less than operand2; otherwise returns false.
operand1 <= operand2
Returns true if operand1 is less than or equal to operand2; otherwise returns false.
operand1 & operand2
Returns the result of a bit-wise AND on the operands (operand1 and operand2).
operand1 ^ operand2
Returns the result of a bit-wise XOR on the operands (operand1 and operand2).
operand1 | operand2
Returns the result of a bit-wise OR on the operands (operand1 and operand2).
~ operand
Returns the bit-wise NOT of the operand.
operand1 << operand2
Returns the result of a bit-wise left shift of operand1 by the number of bits specified by operand2. Zeros are shifted in from the right.
operand1 >> operand2
Returns the result of a bit-wise (sign propagating) right shift of operand1 by the number of bits specified by operand2.
operand1 >>> operand2
Returns the result of a bit-wise (zero filling) right shift of operand1 by the number of bits specified by operand2. Zeros are shifted in from the left.
expression ? resultIfTrue : resultIfFalse
This operator evaluates its first operand, the expression. If the expression is true, the value of the second operand (resultIfTrue) is returned; otherwise the value of the third operand (resultIfFalse) is returned.
expression1, expression2
This operator evaluates its first and second operand (expression1 and expression2) and returns the value of the second operand (expression2).
The comma operator can be subtle and is best reserved only for use in argument lists.
var variable = function( optArguments ) { Statements }
This operator is used to create anonymous functions. Once assigned, the variable is used like any other function name, example variable (1, 2, 3). Specify the argument names (in optArguments) if named arguments are required. If no optArguments are specified, arguments may still be passed and will be available using the arguments list.
The JavaScript function operator supports closures, for example:
function make_counter( initialValue ) { var current = initialValue; return function( increment ) { current += increment; return current; } } // ... var counterA = make_counter( 3 ); // Start at 3. var counterB = make_counter( 12 ); // Start at 12. var x1 = counterA( 2 ); // Adds 2, so x1 == 5 var x2 = counterB( 2 ); // Adds 2, so x2 == 14 var x3 = counterA( 7 ); // Adds 7, so x3 == 12 var x4 = counterB( 30 ); // Adds 30, so x4 ==44
Note that for each call to make_counter(), the anonymous function that is returned has its own copy of current (initialized to the initialValue), which is incremented independently of any other anonymous function's current. It is this capturing of context that makes the function that is returned a closure.
See also function declaration and Function type.
property in Object
Returns true if the given Object has the given property; otherwise returns false.
object instanceof type
Returns true if the given object is an instance of the given type, (or of one of its base classes); otherwise returns false.
var instance = new Type( optArguments );
This function calls the constructor for the given Type, passing it the optional arguments (optArguments) if any and returns an instance of the given Type. The Type may be one of the built-in types, one of the library types, or a user-defined type.
Example:
var circle = new Circle( x, y ); var file = new File();
this.property
The "this" operator may only be used within a function that is defined within a class, that is a member function. Within the scope of the function this is a reference to the particular instance (object) of the class's type.
Example:
class MinMax { var _min; var _max; function MinMax( min, max ) { this._min = min; this._max = max; } function max() { return this._max; } function min() { return this._min; } function setMax( value ) { this._max = value; } function setMin( value ) { this._min = value; } }
typeof item
This operator returns a type of the object as a string.
Example:
var f = new Function("arguments[0]*2"); // "object" var str = "text"; // "string" var pi = Math.PI; // "number"
Functions and built-in objects have a "typeof" of "function".