SPS Home    >   Dgreath    >   JavaScript    >   Operators

JAVASCRIPT OPERATORS

The various operators in JavaScript are described below. All operands can be either a literal or a variable. JavaScript operators are used as infix operators between a pre-operand on the left and a post-operand on the right. See JavaScript Identifiers for information about variable operands.

Arithmetic Operators

The following operators provide basic arithmetic operations and are appropriate for Number operand types:
[  +  ] The addition operator adds the first (left) operand to the second (right) operand.
X = Y + 3 thus for Y = 5, X = 8
[  -  ] The subtraction operator subtracts the second (right) operand from the first (left) operand.
X = Y - 3 thus for Y = 8, X = 5
[  *  ] The multiplication operator multiplies the first (left) operand by the second (right) operand.
X = Y * 3 thus for Y = 5, X = 15
[  /  ] The division operator divides the first (left) operand by the second (right) operand.
X = Y / 3 thus for Y = 15, X = 5
[  %  ] The modulus operator computes the integer remainder that results when the first operand is divided by the second operand.
X = Y % 5 thus for Y = 12, X = 2
[  -  ] The unary operator precedes its one operand and negates its value.
X = -Y thus for Y = 3, X = -3
[ ++ ] The increment operator adds a value of one to its operand. When used prefix (i.e. ++Y), it returns the value after incrementing. When used postfix (i.e. Y++), it returns the value before incrementing.
X = ++Y thus for Y = 3, X = 3 and Y = 4
X = Y++ thus for Y = 3, X = 4 and Y = 4
[  --  ] The decrement operator subtracts one from its operand. When used prefix (i.e. --Y), it returns the value after decrementing. When used postfix (i.e. Y--), it returns the value before decrementing.
X = --Y thus for Y = 3, X = 3 and Y = 2
X = Y-- thus for Y = 3, X = 2 and Y = 2
More advanced mathematical operations are available via the properties and methods of the Math class.

Bitwise Operators

The following operators provide bitwise operations:
[ & ] The bitwise AND operator performs a bitwise AND of the value of the first (left) operand to the value of the second (right) operand. It returns a one in each bit position if the corresponding bits of both (left and right) operands are set to one. It returns a zero in each bit position if either or both of the corresponding bits of either (left and/or right)) operand is zero.
[ ^ ] The bitwise XOR operator performs a bitwise XOR of the value of the first (left) operand to the value of the second (right) operand. It returns a one in each bit position if one or the other corresponding bits of  both (left or right) operands are a one but not if both (left and right) are ones. It returns a zero in each bit position if the corresponding bit of both (left and right) operands are either both ones or both zeros. 
[ | ] The bitwise OR operator performs a bitwise OR of the value of the first (left) operand to the value of the second (right) operand. It returns a one in each bit position if either or both of the corresponding bits of  both (left and/or right) operands are a one. It returns a zero in each bit position if the corresponding bit of both (left and right) operands are both zeros.
[ ~ ] The bitwise NOT operator performs a bitwise NOT (bits are flipped) of the value of the operand. It returns a one in each bit position if the corresponding bit is a zero. It returns a zero in each bit position if the corresponding bit is a one.
[ << ] The logical left shift operator performs a bitwise left shift of the value of the first (left) operand according to the value specified in the second (right) operand. Zeros are shifted in from the right.
[ >> ] The bitwise right shift operator performs a bitwise sign propagating right shift of the value of the first (left) operand according to the value specified in the second (right) operand. Bits shifted out from the right are discarded.
[ >>> ] The bitwise right shift operator performs a bitwise zero fill right shift of the value of the first (left) operand according to the value specified in the second (right) operand. Zeros are shifted in from the left and bits shifted out from the right are discarded..

Assignment Operators

The following operators provide basic arithmetic and bitwise operations with the result always assigned to the first operand. The second operand is always left unchanged:
[  =  ] The general assignment operator assigns (sets) the value of the second (right) operand to the first (left) operand. 
X = Y thus for Y = 3, X = 3
[  +=  ] The addition assignment operator adds the value of the second (right) operator to the value of the first (left) operator and the result is assigned to the first (left) operator.  
X += Y is equivalent to X = X + Y
[  -=  ] The subtraction assignment operator subtracts the value of the second (right) operand from the value of the first (left) operand and the result is assigned to the first (left) operand. 
X -= Y is equivalent to X = X - Y
[  *=  ] The multiplication assignment operator multiplies the value of the first (left) operand by the value of the second (right) operand and the result is assigned to the first (left) operand. 
X *= Y is equivalent to X = X * Y
[  /=  ] The division assignment operator divides the value of the first (left) operand by the value of the second (right) operand and the result is assigned to the first (left) operand. 
X /= Y is equivalent to X = X / Y
[  %=  ] The modulus assignment operator divides the value of the first (left) operand by the value of the second (right) operand and the remainder is assigned to the first (left) operand. 
X %= Y is equivalent to X = X % Y
[  &=  ] The bitwise logical AND assignment operator performs a bitwise AND of the value of the first (left) operand to the value of the second (right) operand and assigns the result to the first (left) operand. It returns a one in each bit position of the first (left) operand if the bits of both (left and right) operands are set to one. It returns a zero in each bit position of the first (left) operand if either or both of the corresponding bits of either (left and/or right) operand is zero.
X &= Y is equivalent to X = X & Y
[  ^=  ] The bitwise logical XOR assignment operator performs a bitwise XOR of the value of the first (left) operand to the value of the second (right) operand and assigns the result to the first (left) operand. It returns a one in each bit position of the first (left) operand if one or the other corresponding bits of both (left and right) operands are a one but not if both are ones. It returns a zero in each bit position of the first (left) operand if the corresponding bit of both (left and right) operands are either both ones or both zeros.
X ^= Y is equivalent to X = X ^ Y 
[  |=  ] The bitwise logical OR assignment operator performs a bitwise OR of the value of the first (left) operand to the value of the second (right) operand and assigns the result to the first (left) operand. It returns a one in each bit position of the first (left) operand if either or both of the corresponding bits of  both (left and/or right) operands are a one. It returns a zero in each bit position of the first (left) operand if the corresponding bit of both (left and right) operands are both zeros.
X |= Y is equivalent to X = X | Y
[  <<=  ] The bitwise logical left shift assignment operator performs a bitwise left shift of the value of the first (left) operand according to the value specified in the second (right) operand and assigns the result to the first (left) operand. Zero bits are shifted in from the right.
X <<= Y is equivalent to X = X << Y 
[  >>=  ] The bitwise logical right shift assignment operator performs a bitwise sign propagating right shift of the value of the first (left) operand according to the value specified in the second (right) operand and assigns the result to the first operand. Bits shifted out of the right are discarded.
X >>= Y is equivalent to X = X >> Y
[  >>>=  ] The bitwise logical right shift assignment operator performs a bitwise zero fill right shift of the value of the first (left) operand according to the value specified in the second (right) operand and assigns the result to the first (left) operand. Zero bits are shifted in from the left and bits shifted out of the right are discarded.
X >>> Y is equivalent to X = X >>> Y 

String Operators

The following operators provide string operations and are appropriate for String operand types:
[ + ] The concatenation operator returns a string consisting of the value of the first (left) operand joined with the value of the second (right) operand.
X = Y + "bar" thus for Y = "foo", X = "foobar"
[ += ] The concatenation assignment operator returns a string consisting of the value of the first (left) operand joined with the value of the second (right) operand and the result is assigned to the first operand.
X += "bar" thus for X = "foo", X = "foobar"

Logical Operators

The following operators provide logical operations:
[ && ] The logical AND operator returns the value of the first (left) operand if it can be converted to false, otherwise it  returns the value of the second (right) operand. When applied to Boolean values, && returns '1' if both operands are true, '0' if either or both are false.
[ || ] The logical OR operator returns the value of the first (left) operand if it can be converted to true, otherwise it returns the value of the second (right) operand. When applied to Boolean values, || returns '1' if either or both operands are true, '0' if both are false.
[ ! ] The logical NOT operator returns '0' if the value of its single operand can be converted to true, otherwise it returns '1'.

Comparison Operators

The following operators provide comparison operations for either string or numerical operand types and produce logical operand types:
[  ==  ] The equal operator returns 'true' if the value of the first (left) operand is equal to the value of the second (right) operand. It returns 'false' if the value of the first (left) operand is not equal to the value of the second (right) operator.  Notes: Two strings are equal when they both possess identical character sequence and length.  Two numbers are equal when they are numerically equal.  NaN is never equal to anything including NaN.  Positive and negative zero values are equal. Two objects are equal if both refer to the same object. Two Boolean operands are equal if both are either TRUE or FALSE.
[  !=  ] The not equal operator returns 'true' if the value of the first (left) operand is not equal to the value of the second (right) operand. It returns 'false' if the value of the first (left) operand is equal to the value of the second (right) operand. Two strings are not equal when they possess different character sequence or length.  Two numbers are not equal when they are numerically different.  NaN is always not equal to anything including NaN.  Two objects are not equal unless both refer to the same object. Two Boolean operands are not equal unless both are either TRUE or FALSE.
[  ===  ] The strict equal operator returns 'true' if the value of the first (left) operand is equal to the value of the second (right) operand and both are the same type. It returns 'false' if the value of the first (left) operand and the value of the second (right) operand are not equal or if the types are mismatched. See notes for the equal operator.
[  !==  ] The strict not equal operator returns 'true' if the value of the first (left) operand is not equal to the value of the second (right) operand or if both are not the same type. It returns 'false' if the value of the first (left) operand is equal to the value of the second (right) operand and the types are matched. See notes for the not equal operator.
[  >  ] The greater than operator returns 'true' if the value of the first (left) operand is greater than the value of the second (right) operand. It returns 'false' if the value of the first (left) operand is less than or equal to the value of the second (right) operand.
[  >=  ] The greater than or equal operator returns 'true' if the value of the first (left) operand is greater than or equal to the value of the second (right) operand.  It returns 'false' if the value of the first (left) operand is less than the value of the second (right) operand.
[   <   ] The less than operator returns 'true' if the value of the first (left) operand is less than the value of the second (right) operand. It returns 'false' if the value if the first (left) operand is greater than or equal to the value of the second (right) operand.
[  <=  ] The less than or equal operator returns 'true' if the value of the first (left) operand is less than or equal to the value of the second (right) operand.  It returns 'false' if the value of the first (left) operand is greater than the value of the second (right) operand.
[ in ] The in operator returns 'true' if the value of the property specified in the first (left) operand is contained in the object specified in the second (right) operand. It returns 'false' if the value of the property specified in the first (left) operand is not contained in the object specified in the second (right) operand:
someProperty in anObject thus where anObject is an array of tree names (e.g. ash, beech, cherry, dogwood), the expression would return 'true' if the value of someProperty happened to be "cherry" and 'false' if it happened to be "elm"
Note: the first operand must be a property, the second operand must be an object.
[ instanceof ] The instanceof operator returns 'true' if the value specified in the first (left) operand is the same type of object contained in the object specified in the second (right) operand. It returns 'false' if the value of the property specified in the first (left) operand is not the same type of object specified in the second (right) operand:
myString instance of String returns 'true'
myDate instance of String returns 'false'
Note: the value of the second operand must be an object.

JavaScript Special Operators

The following operators provide special operations and are used with properties, statements, and functions:
[ ?: ] The conditional operator is the sole JavaScript operator having three operands (i.e. condition ? expr1 : expr2). If condition is TRUE, the operator returns the value of expr1. If condition is FALSE, the operator returns the value of expr2.
[ , ] The comma operator evaluates both of its operands and returns the result of the second operand. It is used to include multiple expressions in a location that normally expects only one such as providing multiple expressions in a for loop:
for (var a=0,b=2; a<=9; a++,b++)
or in the argument/parameter list of a function.
[ . ] The dot operator connects properties and methods to their object.
window.open()
[ : ] The label (colon) operator allows a label to be prefixed to a statement to provide a target for either a break or continue statement:
begin: myString += addString;
[ ; ] The statement separator (semicolon) operator separates individual statements from each other within a function.
{ statement ; statement ; statement }
Note that the statement separator is never positioned adjacent to a curly brace.
[ 'foo' ] The string literal operator encloses a string. Typically, these are used inside the double quotation marks when quoting a quote.
[ "foo" ] The string literal operator encloses a string.
[ [foo] ] The bracket operator encloses an array index.
[ (foo) ] The parenthesis operator encloses the arguments of a function.
[ {foo} ] The curly brace operator encloses the methods of a function.
[ // ] The comment operator marks all text to the end of the line as a non-executable comment. It is used to aid in the understanding of the code.
[ /* ] The begin comment operator marks all text to the end comment operator as a non-executable comment. It is used to comment out code during debugging since it can span multiple lines of code.
[ */ ] The end comment operator marks all text from the begin comment operator as a non-executable comment. It is used to comment out code during debugging.
[ typeof ] The typeof operator returns a string indicating the type of the unevaluated operand. The operand may be a string, variable, or object. Results are object, function, number, string, undefined, and boolean.
[ new ] The new operator creates new instances of objects derived from classes or other objects.
[ delete ] The delete operator removes instances of objects.
[ void ] The void operator specifies an expression to be evaluated without returning a value. This is similar to calling a function constructed without a return and is especially useful for simple expressions used in event handlers. Its single operand is the expression to be evaluated:
<a href="javascript:void(document.form.submit())">Click here to submit</a>
Note: while the use of parenthesis is not required, it is encouraged to improve readability.
[ <foo> ] The XML/XHTML element delimiter is not JavaScript! These must be handled as String literals. In most cases, it is better to use '&lt;' in place of '<', '&gt;' in place of '>' and '&47;' in place of '/'. Note also that when specifying element attributes (i.e. align="center") as part of a string literal, the quotation marks would need to be replaced with single quotes, or escaped (i.e. \"), or use '&quot;' since JS would interpret the double quote as a string delimiter. This is a common error to watch out for.

Calling Functions (User Defined Operators/Methods)

A function must exist before it can be called. If the function doesn't exist, JavaScript will return a string representation of the function,  NaN, or undefined depending on the context. Since JavaScript is interpreted rather than compiled, function declarations need to precede the function call. Thus, any function declared in the <head></head> portion of a <html></html> document is available to any function called in the <body</body> portion. However, it should never be assumed that functions declared in the <body></body> portion are similarly available. This is because html markup is parsed as an XML datastructure and it is possible that the referenced function hasn't been parsed at the time of execution. This can lead to unpredictable script behavior that is tough to fix.

Functions can exist in two contexts, first, as a method of an object and second, as a function within an object. In either case, the function name is followed by a  comma delimited list of its arguments enclosed in parentheses [i.e. myFunction(argument1, argument2, argument3) ]. The arguments thus passed to the function will become its various parameters when it executes. The argument list must have the exact quantity of arguments the function expects, in the correct order. Each argument must be of the type expected as well.

Method WITHIN an object
When calling a method within an object, the object's name is assumed, so the function name and parenthetical argument list is sufficient.
Example [open method of the window object]:

myWindow = open("mywin.htm","secondWindow","width=250")
Here the document myWin.htm is opened into a new window with a width of 250 pixels. The new window is named secondWindow.

Method OF an object
When calling the method of an object, the object's name will precede the method name, separated by the dot operator. Its parenthetical argument list will immediately follow.
Example (document.write method of the new myWindow object):

myWindow.document.write(myWindow.name) Here the name of the new window (i.e. secondWindow ) is displayed in the newly created window.