**avascript Operators:**

Arithmetic | Comparison | Boolean | Assignment | Conditional | Void |

Javascript expressions may contain different types of operators.

Eg:

c = a + b // Here ‘+’ is an operator.

c = a – b // Here ‘-‘ is an operator.

Javascript has binary and unary operators:

- A binary operator requires two operands, one before the operator and one after the operator.Â Eg.Â Â a-bÂ Â orÂ Â a/b
- A unary operatorÂ Eg. age++, ++age

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. TheÂ arithmetic operators are:

+ | AdditionÂ Eg: 1+2 returns 3 |

– | SubtractionÂ Eg: 2-1 returns 1 |

/ | DivisionÂ Eg: 4 / 2Â returns 2 |

* | MultiplicationÂ Eg: 2*2 returns 4 |

% | Modulus: the remainder after division Eg: 23 % 3 returns 1. |

++ | Unary increment:Â this operator requires a single operand, either before or after the operator. The operand’s value is increased by 1. The value returned depends on whether the ++ operator is placed before or after the operand.E.g: If a is 1, then ++a sets x to 2 and returns 4, whereas a++ sets a to 2 and returns 1. |

— | Unary decrement: this operator only takes one operand. The operand’s value is decreased by 1. The value returned depends on whether the — operator is placed before or after the operand;e.g.If a is 2, then –a sets a to 1 and returns 1, whereas a– sets a to 1 and returns 2. |

**Javascript Comparison Operators**

A comparison operator can return a true or false value. It compares its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical or string values. When used on string values, the comparisons are based on the standard alphabetic ordering.

== (Equal to ) |
Â returns true if operands are equal.Â |

!=Â (Not equal to) |
returns true if operands are not equal.Â |

> (Greater than) |
Â returns true if left operand is greater than right operand.Â |

>= (Greater than or equal to) |
Â returns true if left operand is greater than or equal to right operand.Â |

< (Less than) |
Â returns true if left operand is less than right operand.Â |

<=Â (Less than or equal to) |
Â returns true if left operand is less than or equal to right operand.Â |

Â A boolean operator takes two operands, each of which is a true or false value, and returns a true or false result.

&& | Â “And” returns true if both operands are true.Â |

|| | Â “Or” returns true if either operand is true.Â |

!Â | “Not” returns true if the negation of the operand is true (e.g. the operand is false).Â |

Assignment operators are required when expressions are evaluated and has to be assigned to a variable. A variable can be assigned to another variable.

= | Assigns the value of the right hand operand to the variable on the left. Eg: a=b |

+=Â | Adds the value of the left hand operand to the right hand variable and assigns to left hand operand. (a +=b means a = a + b) |

Â -= | The value of the right hand operand is subtracted from left handÂ variable and assigns to left hand operand. (a -= b means a = a – b) |

*= | Multiply the value of theÂ left hand operand with the right hand variable and assigns to left hand operand. (a *=b means a = a * b) |

Â /= | Divide the left hand operand with theÂ right hand variable and assigns to left hand operand. (a /= b means a = a / b) |

&=Â | Assigns result of (left hand operand &Â right hand operand) to left hand operand. & stands for AND (a&=b means a=a&b) |

|= | Assigns result of (left hand operand | right hand operand) to left hand operand. | Stands for OR (a|=b means a=a|b) |

x %= yÂ | Assigns result of (left hand operand % right hand operand) to left hand operand. % is modulo operator. Eg: a=a%b means , remainder of (a/b)Â is computed and assigned to aÂ a = a % b |

**Javascript Conditional operator**

The conditional operator has 3 operands. If condition is true, the operator has the value of first expression which comes after ‘?’Â Else, the expression or value after ‘:’ is taken.

The syntax is

(condition) ? expression1 : expression2

Eg:

Adult_or_Teen = (Age >= 21) ? “Adult” : “Teen”

If (Age >=21), variable Adult_or_TeenÂ will be *Adult*, else *Teen* is assigned to variable Adult_or_Teen

The void operator specifies an expression to be evaluated without returning a value.

The void operator is used in either of the following ways:

- javascript:void (expression)
- javascript:void expression

where *expression* is a Javascript expression to evaluate.

The following code creates an link but does nothing when clicked on it.

<A HREF=”javascript:void(null)”>Hey Do nothing !</A>

Eg:

Hey Do nothing !

The following code creates a hypertext link that submits a form when the user clicks it.

<A HREF=”javascript:void(document.form.submit())”>Click here to submit</A>