Thursday 15 May 2014

Javascript Operators

What is an operator?

Operator are simple deifen as an example given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator.

Types of operators:-

-Arithmetic Operators

-Comparision Operators

-Logical (or Relational) Operators

-Assignment Operators

-Conditional (or ternary) Operators

Now hava look i am explaing the operators one by one,

1) Arithmetic Operators-
Assume variable A holds 10 and variable B holds 20 then

Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9


Note: Addition operator (+) works for Numeric as well as Strings. e.g. "a" + 10 will give "a10".

2) Comparision Operators-

Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

3)Logical (or Relational) Operators-


Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A && B) is true.
|| Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is false.

4) Assignment Operators-


Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A

Note: Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, |= and ^=.

5) Conditional (or ternary) Operators -

There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditioanl operator has this syntax:

Operator Description Example
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

Posted by Amit Tiwari

No comments:

Post a Comment