Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
637
JavaScript Operators
Introduction
Operators are basically used to modify the value of a
variable. They are basically used the same like we do in real mathematic. For
example in x = x +2 the x is a variable and the = and + are both operators. You
can use operators also to compare values. For that you can use the < or >
operator. The following table shows all available operators.
Arithmetic Operators
Operator:
Description:
Example:
+
Can be used to add to values.
X=2
X=X+1
Returns 3
-
Can be used to subtract the value.
X=2
X=X-1
Returns 1
*
Can be used to multiply a value.
X=2
pi = 3.14
X=x*pi
Returns 6.28
/
Can be used to divide a value.
X=10
X=10/2
Returns 5
%
Can be used to get the modules of two divided values also
known as the division remainder.
X=10
X=X%2
Returns 0
++
Can be used to increment a value.
X=2
X++
Returns 3
--
Can be used to decrement a value.
X=2
X--
Returns 1
-
Can be also used to return the negation of its operand.
X=2
X=-X
Returns -2
Bitwise Operators
Operator:
Description:
Example:
& (Bitwise AND)
Returns a one in each bit position for which the
corresponding bits of both operands are ones.
10&10
Returns 10
| (Bitwise OR)
Returns one for each bit position if either one or both
bits are set to 1.
10|9
Returns 11
^ (Bitwise XOR)
Returns one for each bit position if one of both bits is
set to one.
10^9
Returns 3
~ (Bitwise NOT)
Inverts the bits of its operand
~9
Returns –10
<< (Left Shift)
Shifts binary bits to left, shifting in zeros from right.
10<<10
Returns 10240
>> (Right Shift)
Shifts binary bits to right, discarding bits shifted off.
10>>10
Returns 0
>>> (Zero-fill right shift)
Shifts binary bits to right, discarding bits shifted off
and shifting in zeros from left.
10>>>2
Returns 2
Comparison Operators
Operator:
Description:
Example
==
Can be used to compare to variables.
2==2
Returns true
===
Can be used to compare the value and the data type.
var i = 0
var x = 0
x === i
Returns true
!=
Can be used to verify if a value is not equal to another
value.
2!=2
Returns false
!==
Can be used to verify the value and the data type aren’t
same.
var i = 0
var x = 0
x !== i
Returns true
>
Can be used to check whether a value is greater then
another value.
var i = 0
var x = 1
i > x
Returns false
<
Can be used to check if a value is less then another
value.
var i = 0
var x = 1
i < x
Returns true
<=
Can be used to check if a value is less or equal to
another value.
var i = 0
var x = 1
i <= x
Returns true
>=
Can be used to if a value is greater or equal to another
value.
var i = 0
var x = 1
i <= x
Returns false
Logical Operators
Operator:
Description:
Example:
&&
Can be used to combine two or more conditions, where all
need to return true.
i = 10
x = 20
x < 30 && i > 0
Returns true
||
Can be used to combine two or more conditions, where one
of them needs to return true.
i = 10
x = 20
x == 30 || i == 0
Returns false
!
Can be used to check if a value is not true.
i = 0
x = 1
!(i == x)
Returns true
String Operators
Operator:
Description:
Example:
+
This operator can be used to concatenate strings.
var i = “Hello”
var x = “World”
i + x
Returns Hello World
Conditional Operator
The conditional operator is another form of an “if”
condition. It takes three parameters. The syntax looks like this: (Condition) ?
val1: val2. If condition is true then it will return val1 otherwise val2.
Example of Condition Operator
The following example checks whether bMarried is true, if so
then it returns “married” otherwise it will return “not married”.