Today is:
    | Home | Web Technology | Language | Articles | | About-Us | Contact-Us |

JS Tutorial     

JavaScript Ch3 - Operators


JavaScript supports both binary and unary operators. Binary operators require that there were two operands in the expression, such as 9 + x, whereas unary operators only need one operand, such as ++ x. JavaScript also supports operators that deal with strings and logical values.

3.1 Assignment Operators

An assignment operator (=) assigns a value to a variable. When JavaScript encounters an assignment operator, it looks to the right for a value, and then to the left for a place (variable) to store it. If the left is a variable, it assigns the value to it. If the left is not storeable (e.g 20 = x), it throws error.

  The Combination of Assignment and Arithmetic Operators

x += y is short for x = x +y
x -= y is short for x = x - y
x *= y is short for x = x*y
x /= y is short for x = x /y
x %= y is short for x = x % y
The Combination of Assignment and Bitwise Operators

x <<= y is short for x = x << y
x >>= y is short for x = >> y
x >>>= y is short for x = x >>> y
x &= y is short for x = x & y
x ^= y is short for x = x ^ y
x |= y is short for x = x | y

3.2 Arithmetic Operators

Most basic operators include:

  • + addition
  • - substraction
  • / division
  • * multiplication
  • % modulus ( x = 10 %3 ==> x = 1)
  • ++ incrementation (++x the same as x = x + 1)
  • -- decrementation (--x the same as x = x -1)

3.4 Comparision Operators

Here is a list of all the comparison operators:

    ==     The equal operator. Returns true if both of its operands are equal.
!= The not-equal operator.Returns true if its operands are not equal.
> The greater-than operator. Returns true if its left operand is greater in value than its right operand.
>= The greater-than-or-equal operator. Returns true if its left operand is greater than or equal to its right operand.
< The less-than operator. Return true if its left operand is less than the value of its right operand.
<= The less-than-or-equal operator. Return true if its left operand is less than or equal to its right operand.

3.5 String Operators

The set of string operators available in JavaScript includes all comparision operators, along with the concatenation operator (+). Using the concatenation operator, you can easily attach strings together to make a longer string.

3.6 Conditional Operators

JavaScript uses the set of two operators, ? and :, to form conditional expressions.

    <condition>?<value1>:<value2>

The conditional expression returns <value1> if the condition is true, otherwise returns <value2>.
For example:

    resultMsg = (numHits==100000)?:"You have won!":"You lost. Try again";
alert(resultMsg);

Provided that numHits is set equal to 100000 elsewhere in the program, this expression returns the string You have won!; othewise, it returns You lost. Try again.

3.7 Boolean Operators

Sytax:
Expression 1 [operator]Expression 2
[operator] Expression:

    &&     The logical AND operator. It returns true if both Expression1 and Expression2 are true. Otherwise, it returns false.
|| The logical OR operator. It returns true if either Expression1 or Expression2 is true. Otherwise it returns false.
! The logical NOT operator. It returns ture if Expression is false. Otherwise, it returns false.

Examples:

  • (1>0) && (2>1) returns true.
  • (1>0) && (2< 1) returns false.
  • (1>0) || (2<1) returns true.
  • (1<0) || (2<0) returns false.
  • !(1>0) returns false.
  • !(1 < 0) returns true.

    References

    (1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.

    (2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code

    (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software.

    Advertisement

puthik.com ©2008