# Operators

Operators and operator precedence in JavaScript

TAGS: JavaScript, Programming, TECH

## Assignment Operators

• =
• The rest are "short hand" for x = x operator y.
• +=.
• -=.
• *=.
• /= .
• %= .
• <<= .
• >>= .
• >>>= .
• &= .
• ^= .
• |= .

## Arithmetic Operators

• +. Addition. Can also be used as a unary operator that can type-convert to a number type.
• -. Subtraction.
• *. Multiplication.
• /. Division.
• %. Modulus: return integer remainder. EG: 13 % 5 returns 3.
• ++. Unary increment operator. Can be used before the operand to return the new result, or after the operand to return the old result.
• --. Unary decrement operator. Can be used before or after the operand.
• -. Unary operator. Changes sign of operand.

## Bitwise Operators

These convert operand(s) to 32 bits and then perform work on them. In the examples provided, the binary is shown instead of the decimal, for the sake of visual clarity.
• &. Bitwise AND, returns 1s for bit positions where both operands are 1, eg 10 & 12 yields 8, since 1010 and 1100 yields 1000.
• |. Bitwise OR, returns 1s for bit positions where either operand is 1. EG: 10 | 12 yields 14, since 1010 and 1100 yields 1110.
• ^. Bitwise XOR, returns 1s for bit positions where either operand is 1 but not both. EG: 10 ^ 12 yields 6, since 1010 and 1100 yields 0110.
• ~. Bitwise NOT, inverts the operand, eg. ~10 yields 5, since 1010 yields 0101.
• <<. Shift binary a to the left b places and pad the right side with b 0s. EG: 9 << 2 yields 36 since 1001 becomes 100100.
• >>. Shift binary a to the right b places and discard the b rightmost digits. EG: 9 >> 2 yields 2 because 1001 becomes 10.
• >>>. Shift binary a to the right b places and discard the b rightmost digits then pad the left with 0s. EG: 9 >> 2 yields 2 because 1001 becomes 10. For non-negative numbers, >> and >>> yield the same results.

## Logical Operators

These are typically used with Boolean operands to return Boolean values but for JS 1.1, some of these operators can also be used to return one of the operands, thus effectively "overloading" these operands to work as yet another if ... else shorthand. This makes the order of operands relevant!
• &&. And. Returns A if A converts to false, otherwise returns B.
if(!A)A;     else B;   //in JS 1.1+
if(!A)false; else (B); //in JS 1.0
false && false //returns false
false && true  //returns false
true && false  //returns false
true && true   //returns true
0 && "bat"     //returns 0
"ape" && 0     //returns 0
"ape" && "bat" //returns "bat"

• ||. Or. Returns A if A converts to true, otherwise returns B.
if(A)A;    else B;   //in JS 1.1+
if(A)true; else (B); //in JS 1.0
false || false //returns false
false || true  //returns true
true || false  //returns true
true || true   //returns true
0 || "bat"     //returns "bat"
"ape" || 0     //returns "ape"
"ape" || "bat" //returns "ape"

• !. Not. A unary operator that inverts the operand. if(a)false;else true;. EG: !(true) yields false.
• !!. Not not. A unary operator that can type-convert to a boolean type. It is really just two ! operators. EG: !!(true) yields true.

## Comparison Operators

• ==. Equal. True if operand values are equal. A data type conversion is automatically performed if needed.
// The following are true because of auto conversion.
5 == "5"
null == undefined;

a2 = new A;
a1 = new A;
a1 == a2; // false because object must have the same address space.

NaN == NaN; // false because NaN is not equal to anything, including NaN!

• !=. Not equal.
NaN != NaN; // true because NaN is not equal to anything, including NaN!

• ===. Strict equal. True if operand values and data types are equal.
5 === "5" // false
null === undefined; // false

a2 = new A;
a1 = new A;
a1 === a2; // false because object must have the same address space.

NaN == NaN; // false because NaN is not equal to anything, including NaN!

• !==. Strict not equal.
• >.
• <.
• >=.
• <=.
• ## String Operators

• +. Note that the plus sign is used as both an arithmetic and a string operator. EGs:
8 + 8        // 16
"8" + 8      // "88"
8 + "8"      // "88"
"8" + "8"    // "88"
8 + 8 + "8"  // "168"
8 + "8" + 8  // "888"
• +=. EG: strResult += "is super".

## Other Operators

• condition ? StatementIfTrue : StatementIfFalse. Conditional operator. The only JavaScript operator to take 3 operands. It is generally used to select between one of two values.
if (condition) {
return exprT;
} else {
return exprF;
}
gender = isMale ? "male" : "female";

• ,. Comma operator. Evaluates both operands but returns the second.
// This prints the values of the diagonal elements in the array:
for (i=0, j=9; i <= 9; i++, j--)
document.writeln("a[" + i + ", " + j + "] = " + a[i, j])

// This returns x, y, z as 7, 5, 7 respectively:
x = ( y = 5, z = 7);

• delete objectName delete objectName.property delete objectName['property'] delete objectName[index] delete property // use only within a with statement.
Deletes an object, an object's property, or an element at a specified index in an array. Returns false only if the property exists and cannot be deleted, otherwise returns true. Can be used on an user-defined implicitly-declared properties but not on pre-defined (language-defined) or explicitly-declared properties. Cannot delete properties inerited from a prototype (although it can be deleted from the prototype (EG: delete Foo.prototype.bar.
• propertyNameOrNumber in objectName. Returns true if the specified property is in the specified object.
• objectName instanceof objectType. Returns true if the specified object is of the specified object type.
• objectName = new objectType (param1 [,param2] ...[,paramN]). Creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
• this. Refers to the object that the current function is a method of. See The this keyword [http://www.quirksmode.org/js/this.html]. EG: onChange="run(this, 12)". EG: function doX(){ this.style.color = 'red'; }.
• typeof expression typeof (expression). Returns string indicating data type. The 6 possible strings returned are: string, number, boolean, object (includes null, arrays, and regexps), function, or undefined. In an ideal world null, array, and regexp would be their own types in order to match the 9 JS Standard objects.
• void (expression) void expression. Evaluates expression but returns nothing, i.e. returns undefined. EG: <a href="void(document.form.submit())">Click here to submit</a>.

## Operator Precedence

From low to high:

• Comma (,)
• Assignment Operators (=, +=, -=, *=, /=, %=)
• Conditional Operators (? :)
• Logical OR (||)
• Logical AND (&&)
• Bitwise OR (|)
• Bitwise XOR (^)
• Bitwise AND (&)
• Comparison: Equality (==, !=)
• Comparison: Relational (<, <=, >, >=)
• Bitwise: Shift (<<, >>, >>>)
• Arithmetic: Addition & Subtraction (+, -)
• Arithmetic: Multiplication, Division, & Modulus (*, /, %)
• Logical & Arithmetic Negation; Arithmetic Incrementation (!, -, ++, --)
• Call & Member ((), [])

GeorgeHernandez.comSome rights reserved