Javascript Bitwise Logical Computation Assignment Operators
Operator Summary
Addition + Operator
Description
Used to sum two numbers or perform string concatenation.
Syntax
result = expression1 + expression2
The + operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
++ and -- Operators
Description
Used to increment or decrement a variable by one.
Syntax 1
result = ++variable
result = --variable
result = variable++
result = variable--
Syntax 2
++variable
--variable
variable++
variable--
The syntax of the ++ and -- operators has these parts:
Part Description result Any variable. variable Any variable.
Remarks
The increment and decrement operators are used as a shortcut to modify the value stored in a variable. The value of an expression containing one of these operators depends on whether the operator comes before or after the variable:
var j, k; k = 2; j = ++k;j is assigned the value 3, as the increment occurs before the expression is evaluated.
Contrast the following example:
var j, k; k = 2; j = k++;Here, j is assigned the value 2, as the increment occurs after the expression is evaluated.
/ Operator
Description
Used to divide two numbers and return a numeric result.
Syntax
result = number1 / number2
The / operator syntax has these parts:
Part Description result Any numeric variable. number1 Any numeric expression. number2 Any numeric expression.
% Operator
Description
Divides two numbers and returns the remainder.
Syntax
result = number1 % number2
The % operator syntax has these parts:
Part Description result Any variable. number1 Any numeric expression. number2 Any numeric expression.
Remarks
The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (which is result) equals 5.
A = 19 % 6.7
* Operator
Description
Multiplies two numbers.
Syntax
result = number1*number2
The * operator syntax has these parts:
Part Description result Any variable. number1 Any expression. number2 Any expression.
- Operator
Description
Used to find the difference between two numbers or to indicate the negative value of a numeric expression.
Syntax 1
result = number1 - number2
Syntax 2
-number
The - operator syntax has these parts:
Part Description result Any numeric variable. number Any numeric expression. number1 Any numeric expression. number2 Any numeric expression.
Remarks
In Syntax 1, the - operator is the arithmetic subtraction operator used to find the difference between two numbers.
In Syntax 2, the - operator is used as the unary negation operator to indicate the negative value of an expression.
, Operator
Description
Causes two expressions to be executed sequentially.
Syntax
expression1, expression2
The , operator syntax has these parts:
Part Description expression1 Any expression. expression2 Any expression.
Remarks
The , operator causes the expressions on either side of it to be executed in left-to-right order, and obtains the value of the expression on the right. The most common use for the , operator is in the increment expression of a for loop. For example:
for (i = 0; i < 10; i++, j++) { k = i + j; }The for statement only allows a single expression to be executed at the end of every pass through a loop. The , operator is used to allow multiple expressions to be treated as a single expression, thereby getting around the restriction.
?: Operator
Description
Executes one of two expressions depending on a condition.
Syntax
test ? expression1 : expression2
The ?: operator syntax has these parts:
Part Description test Any Boolean expression. expression1 An expression executed if test is true. expression2 An expression executed if test is false.
Remarks
The ?: operator is a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward. For example:
var now = new Date(); var greeting = "Good" + ((now.getHours() > 17) ? " evening." : " day.");The example creates a string containing "Good evening." if it is after 6pm. The equivalent code using an if...else statement would look as follows:
var now = new Date(); var greeting = "Good"; if (now.getHours() > 17) greeting += " evening."; else greeting += " day.";
Comparison Operators
Description
Returns a Boolean value indicating the result of the comparison.
Syntax
expression1 comparisonoperator expression2
The Comparison operator syntax has these parts:
Part Description expression1 Any expression. comparisonoperator Any comparison operator. expression2 Any expression.
Remarks
When comparing strings, JScript uses the Unicode character value of the string expression.
The following describes how the different groups of operators behave depending on the types and values of expression1 and expression2:
Relational (<, >, <=, >=)
- Attempt to convert both expression1 and expression2 into numbers.
- If both expressions are strings, do a lexicographical string comparison.
- If either expression is NaN, return false.
- Negative zero equals Positive zero.
- Negative Infinity is less than everything including itself.
- Positive Infinity is greater than everything including itself.
Equality (==, !=)
- If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.
- NaN is not equal to anything including itself.
- Negative zero equals positive zero.
- null equals both null and undefined.
- Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.
- Every other comparison is considered unequal.
Identity (===. !==)
These operators behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal.
&& Operator
Description
Performs a logical conjunction on two expressions.
Syntax
result = expression1 && expression2
The && operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
If, and only if, both expressions evaluate to True, result is True. If either expression evaluates to False, result is False.
For information on when a run-time error is generated by the && operator, see the Operator Behavior table.
JScript uses the following rules for converting non-Boolean values to Boolean values:
- All objects are considered true.
- Strings are considered false if, and only if, they are empty.
- null and undefined are considered false.
- Numbers are false if, and only if, they are zero.
! Operator
Description
Performs logical negation on an expression.
Syntax
result = !expression
The ! operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
The following table illustrates how result is determined.
If expression is Then result is True False False True All unary operators, such as the ! operator, evaluate expressions as follows:
- If applied to undefined or null expressions, a run-time error is raised.
- Objects are converted to strings.
- Strings are converted to numbers if possible. If not, a run-time error is raised.
- Boolean values are treated as numbers (0 if false, 1 if true).
The operator is applied to the resulting number.
For the ! operator, if expression is nonzero, result is zero. If expression is zero, result is 1.
|| Operator
Description
Performs a logical disjunction on two expressions.
Syntax
result = expression1 || expression2
The || operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
If either or both expressions evaluate to True, result is True. The following table illustrates how result is determined:
If expression1 is And expression2 is The result is True True True True False True False True True False False False For information on when a run-time error is generated by the && operator, see the Operator Behavior table.
JScript uses the following rules for converting non-Boolean values to Boolean values:
- All objects are considered true.
- Strings are considered false if and only if they are empty.
- null and undefined are considered false.
- Numbers are false if, and only if, they are 0.
& Operator
Description
Performs a bitwise AND on two expressions.
Syntax
result = expression1 & expression2
The & operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The & operator looks at the binary representation of the values of two expressions and does a bitwise AND operation on them. The result of this operation behaves as follows:
0101 (expression1) 1100 (expression2) ---- 0100 (result)Any time both of the expressions have a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
<< Operator
Description
Shifts the bits of an expression to the left.
Syntax
result = expression1 << expression2
The << operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The << operator shifts the bits of expression1 left by the number of bits specified in expression2. For example:
var temp temp = 14 << 2The variable temp has a value of 56 because 14 (00001110 in binary) shifted left two bits equals 56 (00111000 in binary).
~ Operator
Description
Performs a bitwise NOT (negation) on an expression.
Syntax
result = ~ expression
The ~ operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
All unary operators, such as the ~ operator, evaluate expressions as follows:
- If applied to >undefined or null expressions, a run-time error is raised.
- Objects are converted to strings.
- Strings are converted to numbers if possible. If not, a run-time error is raised.
- Boolean values are treated as numbers (0 if false, 1 if true).
The operator is applied to the resulting number.
The ~ operator looks at the binary representation of the values of the expression and does a bitwise negation operation on it. The result of this operation behaves as follows:
0101 (expression) ---- 1010 (result)Any digit that is a 1 in the expression becomes a 0 in the result. Any digit that is a 0 in the expression becomes a 1 in the result.
| Operator
Description
Performs a bitwise OR on two expressions.
Syntax
result = expression1 | expression2
The | operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The | operator looks at the binary representation of the values of two expressions and does a bitwise OR operation on them. The result of this operation behaves as follows:
0101 (expression1) 1100 (expression2) ---- 1101 (result)Any time either of the expressions has a 1 in a digit, the result will have a 1 in that digit. Otherwise, the result will have a 0 in that digit.
>> Operator
Description
Shifts the bits of an expression to the right, maintaining sign.
Syntax
result = expression1 >> expression2
The >> operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The >> operator shifts the bits of expression1 right by the number of bits specified in expression2. The sign bit of expression1 is used to fill the digits from the left. Digits shifted off the right are discarded. For example, after the following code is evaluated, temp has a value of -4: 14 (11110010 in binary) shifted right two bits equals -4 (11111100 in binary).
var temp temp = -14 >> 2
Description
Performs a bitwise exclusive OR on two expressions.
Syntax
result = expression1 ^ expression2
The ^ operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The ^ operator looks at the binary representation of the values of two expressions and does a bitwise exclusive OR operation on them. The result of this operation behaves as follows:
0101 (expression1) 1100 (expression2) ---- 1001 (result)When one, and only one, of the expressions has a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
>>> Operator
Description
Performs an unsigned right shift of the bits in an expression.
Syntax
result = expression1 >>> expression2
The >>> operator syntax has these parts:
Part Description result Any variable. expression1 Any expression. expression2 Any expression.
Remarks
The >>> operator shifts the bits of expression1 right by the number of bits specified in expression2. Zeroes are filled in from the left. Digits shifted off the right are discarded. For example:
var temp temp = -14 >>> 2The variable temp has a value of 1073741820 as -14 (11111111 11111111 11111111 11110010 in binary) shifted right two bits equals 1073741820 (00111111 11111111 11111111 11111100 in binary).
= Operator
Description
Assigns a value to a variable.
Syntax
result = expression
The = operator syntax has these parts:
Part Description result Any variable. expression Any numeric expression.
Remarks
As the = operator behaves like other operators, expressions using it have a value in addition to assigning that value into variable. This means that you can chain assignment operators as follows:
j = k = l = 0;j, k, and l equal zero after the example statement is executed
delete Operator
Description
Deletes a property from an object, or removes an element from an array.
Syntax
delete expression
Where expression is a valid JScript expression that usually (but does not have to) result in a property name or array element.
Remarks
If the result of expression is an object, the property specified in expression exists, and the object will not allow it to be deleted, false is returned.
In all other cases, true is returned.
instanceof Operator
Description
Returns a Boolean value that indicates whethor or not an object is an instance of a particular class.
Syntax
result = object instanceof class
The instanceof operator syntax has these parts:
Part Description result Any variable. object Any object expression. class Any defined object class.
Remarks
The instanceof operator returns true if object is an instance of class. It returns false if object is not an instance of the specified class, or if object is null.
The following example illustrates the use of the instanceof operator:
function objTest(obj) { var i, t, s = ""; // Create variables. t = new Array(); // Create an array. t["Date"] = Date; // Populate the array. t["Object"] = Object; t["Array"] = Array; for (i in t) { if (obj instanceof t[i]) // Check class of obj. { s += "obj is an instance of " + i + "\n"; } else { s += "obj is not an instance of " + i + "\n"; } } return(s); // Return string. } var obj = new Date(); response.write(objTest(obj));
new
Operator
Description
Creates a new object.
Syntax
new constructor[(arguments)]
The constructor argument calls object's constructor. The parentheses can be omitted if the constructor takes no arguments.
Remarks
The new operator performs the following tasks:
- It creates an object with no members.
- It calls the constructor for that object, passing a pointer to the newly created object as the this pointer.
The constructor then initializes the object according to the arguments passed to the constructor.
These are examples of valid uses of the new operator:
my_object = new Object; my_array = new Array(); my_date = new Date("Jan 5 1996");
typeof Operator
Description
Returns a string that identifies the data type of an expression.
Syntax
typeof [ ( ] expression [ ) ] ;
The expression argument is any expression for which type information is sought.
Remarks
The typeof operator returns type information as a string. There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."
The parentheses are optional in the typeof syntax.
void Operator
Description
Prevents an expression from returning a value..
Syntax
void expression
The expression argument is any valid JScript expression.
Remarks
The void operator evaluates its expression, and returns undefined. It is most useful in situations where you want an expression evaluated but do not want the results visible to the remainder of the script.
+= Operator
Description
Used to increment a variable by a specified amount.
Syntax
result += expression
The += operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using this operator is exactly the same as specifying:
result = result + expressionThe underlying subtype of the expressions determines the behavior of the += operator.
If Then Both expressions are numeric or Boolean Add. Both expressions are strings Concatenate. One expression is numeric and the other is a string Concatenate.
&= Operator
Description
Used to perform a bitwise AND on an expression.
Syntax
result &= expression
The &= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using this operator is exactly the same as specifying:
result = result & expressionThe &= operator looks at the binary representation of the values of result and expression and does a bitwise AND operation on them. The output of this operation behaves like this:
0101 (result) 1100 (expression) ---- 0100 (output)Any time both of the expressions have a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
|= Operator
Description
Used to perform a bitwise OR on an expression.
Syntax
result |= expression
The |= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using this operator is exactly the same as specifying:
result = result | expressionThe |= operator looks at the binary representation of the values of result and expression and does a bitwise OR operation on them. The result of this operation behaves like this:
0101 (result) 1100 (expression) ---- 1101 (output)Any time either of the expressions has a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
^= Operator
Description
Used to perform a bitwise exclusive OR on an expression.
Syntax
result ^= expression
The ^= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using the ^= operator is exactly the same as specifying:
result = result ^ expressionThe ^= operator looks at the binary representation of the values of two expressions and does a bitwise exclusive OR operation on them. The result of this operation behaves as follows:
0101 (result) 1100 (expression) ---- 1001 (result)When one, and only one, of the expressions has a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
/= Operator
Description
Used to divide a variable by an expression.
Syntax
result /= expression
The /= operator syntax has these parts:
Part Description result Any numeric variable. expression Any numeric expression.
Remarks
Using the /= operator is exactly the same as specifying:
result = result / expression
<<= Operator
Description
Used to shift the bits of an expression to the left.
Syntax
result <<= expression
The <<= operator syntax has these parts:
Part Description result Any variable. expression Any >expression.
Remarks
Using the <<= operator is exactly the same as specifying:
result = result << expressionThe <<= operator shifts the bits of result left by the number of bits specified in expression. For example:
var temp temp = 14 temp <<= 2The variable temp has a value of 56 because 14 (00001110 in binary) shifted left two bits equals 56 (00111000 in binary). Bits are filled in with zeroes when shifting.
%= Operator
Description
Used to divide two numbers and return only the remainder.
Syntax
result %= expression
The %= operator syntax has these parts:
Part Description result Any variable. expression Any numeric expression.
Remarks
Using the %= operator is exactly the same as specifying:
result = result % expression
*= Operator
Description
Used to multiply a number by another number.
Syntax
result *= expression
The *= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using the *= operator is exactly the same as specifying:
result = result * expression
>>= Operator
Description
Used to shift the bits of an expression to the right, preserving sign.
Syntax
result >>= expression
The >>= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using the >>= operator is exactly the same as specifying:
result = result >> expressionThe >>= operator shifts the bits of result right by the number of bits specified in expression. The sign bit of result is used to fill the digits from the left. Digits shifted off the right are discarded. For example, after the following code is evaluated, temp has a value of -4: 14 (11110010 in binary) shifted right two bits equals -4 (11111100 in binary).
var temp temp = -14 temp >>= 2
-= Operator
Description
Used to subtract the value of an expression from a variable.
Syntax
result -= expression
The -= operator syntax has these parts:
Part Description result Any numeric variable. expression Any numeric expression.
Remarks
Using the -= operator is exactly the same as doing the following:
result = result - expression
>>>= Operator
Description
Used to make an unsigned right shift of the bits in a variable.
Syntax
result >>>= expression
The >>>= operator syntax has these parts:
Part Description result Any variable. expression Any expression.
Remarks
Using the >>>= operator is exactly the same as doing the following:
result = result >>> expressionThe >>>= operator shifts the bits of result right by the number of bits specified in expression. Zeroes are filled in from the left. Digits shifted off the right are discarded. For example:
var temp temp = -14 temp >>>= 2The variable temp has a value of 1073741820 as -14 (11111111 11111111 11111111 11110010 in binary) shifted right two bits equals 1073741820 (00111111 11111111 11111111 11111100 in binary).

