The most common operators are mathematical operators; +, -, /, * (add, subtract, divide, multiply) for example. Operators can be split into two groups, comparison operators and assignment or ‘action’ operators. Comparison operators test to see if two variables relate to each other in the specified way, for example, one variable is a higher number than the other. Other operators perform an action on a variable, such as increasing it by one.
The following table gives those that are most commonly used. The JavaScript 1.3 operators such as the identity operator === are supported by all current browsers, but are not used as often as the others. Old browsers that do not understand them will just produce errors.

JavaScript operators

Operator
Uses
+
adds two numbers or appends two strings – if more than one type of variable is appended, including a string appended to a number or vice-versa, the result will be a string
-
subtracts the second number from the first
/
divides the first number by the second
*
multiplies two numbers
%
divide the first number by the second and return the remainder
=
assigns the value on the right to the object on the left
+=
the object on the left = the object on the left + the value on the right – this also works when appending strings
-=
the object on the left = the object on the left – the value on the right
> 
number on the left must be greater than the number on the right – this also works with strings and values
< 
number on the left must be less than the number on the right – this also works with strings and values
>=
number on the left must be greater than or equal to the number on the right – this also works with strings and values
<=
number on the left must be less than or equal to the number on the right – this also works with strings and values
++
increment the number
--
decrement the number
==
the numbers or objects or values must be equal
!=
the numbers or objects or values must not be equal
<< 
bitwise leftshift
>> 
bitwise rightshift
&
bitwise AND
|
bitwise OR
^
bitwise XOR
~
bitwise NOT
!
logical NOT (the statement must not be true)
&&
logical AND (both statements must be true)
||
logical OR (either statement must be true)
in
object or array on the right must have the property or cell on the left
===
the numbers or objects or values must be equal, and must be the same variable type
!==
the numbers or objects or values must not be equal, or must not be the same variable type
Example of Addition Assignment:
<html>
<script language="JavaScript">
<!--
num = 42;
str = new String("42");
num += 8;
str += 8;
document.write("num = ",num,"<br>str = ",str);
-->
</script>
</html>

or
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var A = "JavaScript ", B = "by Example!";
C = A + B;
alert(C);
//  -->
</script>
</head>
<body>

</body>
</html>

0 comments:

Post a Comment

 
Top
Blogger Template