Operators Summarized

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

Operators Summarized

Rating:


The numeric operators: The numeric operators are used for various types of calculations.

The following are the numeric operators:

  1. The addition operator (+): The addition operator is a binary operator that is used to add two or more numeric values and produce the sum.

    The following example will demonstrate the use of the addition operator:

    var num1=54;
    var num2=10;
    var sum=num1+num2;
    trace(sum);
    //output: 64


    The addition operator can also be used to concatenate strings. The following example will demonstrate this:

    var str1="Steve";
    var str2=32;
    var str3="Name="+str1+" "+"Age="+str2
    alert(str3);
    //output: Name= Steve Age= 32


  2. The subtraction operator (-): The subtraction operator is a binary operator that is used to calculate the difference of two numbers.

    The following example will demonstrate the use of the subtraction operator:

    var num1=54;
    var num2=10;
    var diff=num1-num2;
    trace(diff);
    //output: 44


  3. The multiply operator (*): The multiply operator is a binary operator that is used to multiply two or more values to get the product of two numbers.

    The following example will demonstrate the use of the multiply operator:

    var num1=54;
    var num2=10;
    var prod=num1*num2;
    trace(prod);
    //output: 540


  4. The divide operator (/): The divide operator is a binary operator that is used to divide two numbers and produce the results.

    The following example will demonstrate the use of the divide operator:

    var num1=54;
    var num2=10;
    var div=num1/num2;
    trace(div);
    //output: 5.4


  5. The modulus operator (%): The modulus operator is a binary operator that is used to find the remainder after the completion of division.

    The following example will demonstrate the use of the modulus operator:

    var num1=54;
    var num2=10;
    var mod=num1%num2;
    trace(mod);
    //output: 4


  6. The increment operator:

    The following example will demonstrate the use of the increment operator:

    var a=10;
    var b;
    ++a
    trace(a); //output: 11
    a++
    trace(a); //output: 12
    b=++a;
    trace(a); //output: 13
    trace(b); //output: 13
    b=a++
    trace(a); //output: 14
    trace(b); //output: 13


  7. The decrement operator: The decrement operator is a unary operator that is used to decrement the value of a variable by one. It has two different behaviors depending on the position of the operator. The two behaviors are as follows:

    1. x--

    2. --x

    Both of the above statements mean x=x-1. But when these are assigned to a variable, the meaning is changed. Let us see this in the following example:

    Suppose the value of x=10, then in the statement b=--x, the value of the variables x and b will be 9. It is so because b=--x contains two statements:

    1. b=x

    2. x=x-1

    Since the statement executes from left to right and the -- operator has more priority than the = operator, x will first be decremented by one, and then its value will be assigned to b.

    In case of the statement b=x--, the value of b will be 10 and the value of x will be 9 because b=x-- contains two statements.

    1. b=x

    2. x=x-1

    In this case, when the statement executes from left to right, it comes in contact with the variable x. The value of x is assigned to b and x is decremented by 1.

    The following example will demonstrate the use of the decrement operator:

    var a=10;
    var b;
    --a
    trace(a); //output: 9
    a--
    trace(a); //output: 8
    b=--a
    trace(a); //output: 7
    trace(b); //output: 7
    b=a--
    trace(a); //output: 6
    trace(b); //output: 7


  8. The negation operator: The negation operator is used to negate a value.

    The following example will demonstrate the use of the negation operator:

    var a=10;
    var b=-a;
    trace(b); //Output: -10


The comparison operators: The comparison operators are used to compare two or more values. The result of this comparison is a boolean value that can be true or false.

The following are the comparison operators:

  1. The greater than operator (>): The greater than operator is used to test whether or not the first operand is greater than the second operand. If the first operand is greater than the second operand, it returns true; otherwise, it returns false.

    The following example will demonstrate the use of the greater than operator:

    var a=10;
    var b=10;
    trace(a>b); //output: False


  2. The greater than or equal to operator (>=): The greater than or equal to operator is used to test whether or not the first operand is greater than or equal to the second operand. If the first operand is greater than or equal to the second operand, it returns true; otherwise, it returns false.

    The following example will demonstrate the use of the greater than or equal to operator:

    var a=10;
    var b=10;
    trace(a>=b); //output: True


  3. The less than operator (<): The less than operator is used to test whether or not the first operand is less than the second operand. If the first operand is less than the second operand, it returns true; otherwise, it returns false.

    The following example will demonstrate the use of the less than operator:

    var a=10;
    var b=10;
    trace(a<b); //output: False


  4. The less than or equal to operator (<=): The less than or equal to operator is used to test whether or not the first operand is less than or equal to the second operand. If the first operand is less than or equal to the second operand, it returns true. If the first operand is greater than the second operand, it returns false.

    The following example will demonstrate the use of the equal operator:

    var a=10;
    var b=10;
    trace(a<=b); //output: True


The assignment operator: The assignment operators are used to assign a value to a variable. These variables can be used in other parts of the program with the same or different values and for different purposes.

The following are the assignment operators:

  1. The equal to operator(=): This operator is used for assigning values to variables. It can also be used to copy the value of a variable to another variable.

    The following example will demonstrate the use of the equal to operator:

    var a=10; // The value 10 is assigned to variable a.
    var b=a; //The value of variable a is assigned to variable b.


  2. The add by value operator (+=): When the add by value operator is used, the operand on the left side is added to the operand on the right side and the result is assigned to the left operand.

    OperationResult
    x+=y x=x+y


    The following example will demonstrate the use of the add by value operator:

    var x=200;
    var y=10;
    x+=y
    trace(x); //output: 210


    The add by value operator can also be used for the concatenation of strings.

    The following code will demonstrate this:

    var str="Hello";
    str+="Steve";
    trace(str);//Output: Hello Steve


  3. The subtract by value operator (-=): When the subtract by value operator is used, the operand on the right side is subtracted to the operand on the left side and the result is assigned to the left operand.

    OperationResult
    x-=y x=x-y


    The following example will demonstrate the use of the subtract by value operator:

    var x=200;
    var y=10;
    x-=y
    trace(x); //output: 190


  4. The multiply by value operator (*=): When the multiply by value operator is used, the operands on the right side is multiplied to the operand on the left side and the result is assigned to the left operand.

    OperationResult
    x*=y x=x*y


    The following example will demonstrate the use of the multiply by value operator:

    var x=200;
    var y=10;
    x*=y
    trace(x); //Output: 2000


  5. The divide by value operator (/=): When the divide by value operator is used, the operand on the right side is divided by the operand on the left side and the result is assigned to the left operand.

    OperationResult
    x/=y x=x/y


    The following example will demonstrate the use of the divide by value operator:

    var x=200;
    var y=10;
    x/=y
    trace(x); //output: 20


  6. The modulus by value operator (%=): This operator performs a modulo operation with both the operands and the result is assigned to the left operand.

    operationResult
    x%=y x=x%y


    The following example will demonstrate the use of the equal operator:
    var x=200;
    var y=10;
    x%=y
    trace(x); //output: 0


  7. The left shift by value operator (<<=): This operator performs a bitwise left shift on expressions and the result is assigned to the left operand.

    Operation Result
    x<<=yx=x<<y


    The following example will demonstrate the use of the left shift by value operator:

    var x=128;
    var y=2,
    x<<=y
    trace(x); //Output: 512


  8. The right shift by value operator (>>=): This operator performs a bitwise right shift on expressions and the result is assigned to the left operand.

    Operation Result
    x>>=y x=x>>y


    The following example will demonstrate the use of the right shift by value operator:

    var x=128;
    var y=2,
    x>>=y
    trace(x); //output: 32


  9. The zero fill by value operator (>>>=) This operator performs a bitwise unsigned right shift on expressions and the result is assigned to the left operand.

    Operation Result
    x>>>=yx=x>>>y


    The following example will demonstrate the use of the zero fill by value operator:

    var x=128;
    var y=2,
    x>>>=y
    trace(x); //output: 32


  10. The bitwise and by value operator (&=) This operator performs a bitwise AND operation on expressions and the result is assigned to the left operand.

    Operation Result
    x&=x=x&y


    The following example will demonstrate the use of the bitwise operator:

    var x=128;
    var y=2,
    x&=y
    trace(x); //output: 0


  11. The bitwise or by value operator (|=) This operator performs a bitwise OR operation on expressions and the result is assigned to the left operand.

    Operation Result
    x|=y x=x|y


    The following example will demonstrate the use of the bitwise or by value operator:

    var x=128;
    var y=2,
    x|=y
    trace(x); //output: 102


  12. The bitwise xor by value operator (^=): This operator performs a bitwise XOR operation on expressions and the result is assigned to the left operand.

    Operation Result
    x^=y x=x^y


    The following example will demonstrate the use of the bitwise xor by value operator:

    var x=128;
    var y=2,
    x^=y
    trace(x); //output: 102


The logical operators:Logical operators are used to combine the results of two conditions to produce a single result or to reverse the result of a single condition. AND (&&), OR (||), and NOT (!) are logical operators.

  1. The AND operato (&&)r: The AND operator evaluates to TRUE if both conditions are TRUE. It evaluates to FALSE if either condition is FALSE.

    Following table will show the result of evaluating two conditions:

    Condition 1And OperatorCondition 2Result
    True &&True True
    True &&False False
    False &&True False
    False &&False False


  2. The OR operator (||): The OR operator evaluates to TRUE if either condition is TRUE. It evaluates to FALSE if both conditions are FALSE.

    The following table will show the result of the or operator:

    Condition 1Or OperatorCondition 2 Result
    True ||True True
    True ||False True
    False ||True True
    False ||False False


  3. The NOT operator (!): the NOT operator is used to reverse the result of a condition. It evaluates to TRUE if the condition is FALSE, and evaluates to FALSE if the condition is TRUE.

    The following table will show the result of the not operator:

    ConditionNot OperatorResult
    True!False
    False !True


The equality Operator: The equality operator is used to test whether operands are equal to each other. The result value is boolean.

  1. The equality operator (==): The quality operator compares both operands. If the operands are equal to each other, the expression evaluates to true; otherwise, it will return false.

    The following example will demonstrate the use of the equality operator:

    trace(7==7);//output: true
    trace(7=="7");//Output: true
    trace("Hello"=="hello");//output: false


  2. The strict equality operator (===): The quality operator compares both operands. The operator first compares data type of both operands, if the operands are of different data type, the expression evaluate false. If the data type is equal, it checks for the value of operands. If the operands are equal to each other, the expression evaluates to true; otherwise, it will return false.

    The following example will demonstrate the use of the strict equality operator:

    trace(7==7)//output: true
    trace(7=="7");//Output: false


  3. The not equal to operator (!=): The not equal to operator is a comparison operator. This operator is used to test the inequality of two operands. If the value of both the operands are not equal, the statement will return true; otherwise, it will return false.

    The following example will demonstrate the use of the not equal to operator:

    trace(x!=y);

    In the above statement, if the value of x is not equal to y, the statement will return true; otherwise, it will return false.


  4. The strict not equal to operator (!==): The strict not equal to operator compares both operands. The operator first compares data type of both operands, if the operands are of different data type, the expression evaluates true. If the data type is equal, it checks for the value of operands. If the operands are not equal to each other, the expression evaluates to true; otherwise, it will return false.

    The following example will demonstrate the use of the strict not equal to operator:

    trace( 4!==3); //output: true
    trace(6!=="6"); //output: true
    trace(6!==6); //output: false


In addition of all the above operators, there are some more operators.

The bitwise operators: The bitwise operators are used to shift the bits of a number according to the operator which changes the value of a number.

The following are the bitwise operators:

  1. The bitwise AND operator (&): This operator produces one if the corresponding bits of both the operands are one.

    The following example will demonstrate the use of the bitwise AND operator:

    var x=3;
    var y=9;
    var z=x&y;
    trace(z); //output: 1


  2. The bitwise OR operator (|): This operator produces one if either or both of the corresponding bits are one.

    The following example will demonstrate the use of the bitwise OR operator:

    var a=128;
    var b=2;
    var c=a | b
    trace(c); //output: 130


  3. The bitwise xor operator (^): It produces one if a bit is zero and the corresponding bit is one. If both bits are one or both bits are zero, it produces zero.

    The following example will demonstrate the use of the bitwise XOR operator:

    var x=30;
    var y=2;
    var z=x^y
    trace(z); //output: 28


  4. The bitwise not operator (~): It inverts all the bits of its operand.

    The following example will demonstrate the use of the bitwise not operator:

    var a=128;
    var c=~a
    trace(c); //output: -129


  5. The bitwise left shift operator (<<): It shifts the bits of a number from right to left and discards the bits that are shifted.

    The following example will demonstrate the use of the bitwise left shift operator:

    var a=128;
    var b=2;
    var c=a<<b
    trace(c); //output: 512


  6. The bitwise right shift operator (>>): It shifts the bits of a number from left to right and discards the bits that are shifted.

    The following example will demonstrate the use of the bitwise right shift operator:

    var a=128;
    var b=2;
    var c=a>>b
    trace(c); //output: 32


  7. The bitwise zero fill operator (>>>): The bitwise zero fill operator shifts zeros from left to right and discards the bits that are shifted.

    The following code will demonstrate the use of the bitwise zero fill operators:

    var a=128;
    var b=2;
    var c=a>>>b
    trace(c); //output: 32


The typeof operator: The typeof operator is used to test the datatype of a value. These datatypes can be a number, string, boolean, object, undefined.

The following code will demonstrate the use of the typeof operator:

var a=2;
var b;
trace(typeof a); //output is number
a="James";
trace(typeof a); // output is string
a=true;
trace(typeof a); // output is Boolean
a=b;
trace(typeof a); // output is undefined


The void operator: The void operator evaluates an expression. Disregards the expression and returns an undefined value.

The following code will demonstrate the use of the void operator:

var x=34;
var y=68;
var z=void(x+y);
trace(z); //output: undefined


The instanceof operator: The instanceof operator is used determine whether the object is a part of the class. If the object is a part of class, the value returned is true, and if the object is not a part of the class, the value retuned is false.

The following code will demonstrate the use of the instanceof operator:

trace( new Array (myArray) instanceof Array); //output: true

The delete operator:

The operator precedence

The operator precedence is an arrangement of operators in AcitonScript. It decides the priority of the operators in a complex statement. If a statement consists of more than one operator, the operators are used according to their priority defined in ActionScript.

The following table displays the operator precedence:

OperatorOperator Name
()Parentheses
[]Braces
!Boolean NOT
~Bitwise NOT
-Negation
++Increment
--Decrement
typeoftypeof
void void
deletedelete
*Multiplication
/Division
%Modulus
+Addition
-Subtraction
<<Bitwise left shift
>>Bitwise right shift
>>>Bitwise zero fill
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
==Equal to (Comparison)
!=Not equal to
&Bitwise AND
^Bitwise XOR
|Bitwise OR
&&Boolean AND
||Boolean OR
?Conditional Operator
=Equal to (Assignment)
*=Multiply by value
/=Divide by value
%=Modulus by value
+=Add by value
-Subtract by value
<<=Left shift by value
>>=Right shift by value
>>>=Zero fill by value
&=Bitwise AND by value
^=Bitwise XOR by value
|=Bitwise OR by value
,comma


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.