Javascript Operators:
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
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. |
Top
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. |
Top
Javascript Boolean
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). |
Top
Javascript Assignment
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 |
Top
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
Top
Javascript void
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>
Top