[JavaScript]
[Previous page] [Section contents page] [Next page]
Operators

Operators serve to combine or relate different pieces of data (remember the rule from the variables page: the two pieces of data should generally be of the same type). The four general kinds of operators in JavaScript are:

  • computational
    +for addition or string concatenation
    -for subtraction or giving a negative value to a number
    *for multiplication
    /for division
    %modulus (computes the remainder for a division operation)
    ++for preincrement or postincrement
    --for predecrement or postdecrement
    An increment or decrement adds or subtracts 1 from some value. The difference between a pre- and postincrement (or decrement) is that the increment (or decrement) in the value occurs before or after the interpretation of the statement in which the increment (or decrement) occurs; an example will help to explain:
    		x = 5
    		y = x++
    		z = ++x
    		
    In this example, after the second statement was interpreted, y would equal to 5 and x would be equal to 6. After the third statement was interpreted, both z and x would be equal to 7.
  • logical (for comparisons)
    ==, !=for determining equality or inequality
    <,>,<=,>=less than, greater than, less than or equal to, greater than or equal to
    !logical not
    &&,||logical and, logical or
    ?conditional selection
    ,logical concatenation (used in control structures; more on this later)
    The ? operator works as follows:
    		newItem = ( someCondition ? thenA : elseB )
    		
    In this example, if someCondition is true, newItem will be equal to thenA; if someCondition is false, newItem will be equal to thenB.
  • bitwise (these deal with the binary representations of data; bitwise operations are for advanced users, and are not covered here)
  • assignment (for assigning values to variables)
    =assignment (don't confuse it with ==!)
    operator=aggregate assignment (more on this later)
    The aggregate assignment operator provides a shortcut for changing the value of a variable; for example:
    		x += 5
    		
    is equivalent to the statement:
    		x = x + 5
    		
[Previous page] [Section contents page] [Next page]