| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
|
JavaScript has following types of operators. In this section describes the operators and contains information about operator precedence.
|
Assignment Operators
Comparison Operators
Arithmetic Operators
Bitwise Operators
Logical Operators
String Operators
|
|
JavaScript has the following types of operators. This section describes the operators and contains information about operator precedence.
|
|
operand1 operator operand2
|
|
|
For example, 3+4 or x*y.
A unary operator requires a single operand, either before or after the operator:
|
|
For example, x++ or ++x.
Java Script has one ternary operator, the conditional operator. A ternary operator requires three operands.
|
| Operator type |
Individual operators |
| member |
. [] |
| call / create instance |
() new |
| negation/increment |
! ~ - + ++ -- typeof void delete |
| multiply/divide |
* / % |
| addition/subtraction |
+ - |
| bitwise shift |
<< >> >>> |
| relational |
< <= > >= in instanceof |
| equality |
== != === !== |
| bitwise-and |
& |
| bitwise-xor |
^ |
| bitwise-or |
| |
| logical-and |
&& |
| logical-or |
|| |
| conditional |
?: |
| assignment |
= += -= *= /= %= <<= >>= >>>= &= ^= |= |
| comma |
, |
|
For example, x++ or ++x.
An assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.
The other assignment operators are shorthand for standard operations, as shown in the following table.
|
| Shorthand operator |
Meaning |
| x += y |
x = x + y |
| x -= y |
x = x - y |
| x *= y |
x = x * y |
| x /= y |
x = x / y |
| x %= y |
x = x % y |
| x <<= y |
x = x << y |
| x >>= y |
x >>= y |
| x >>>= y |
x = x >>> y |
| x &= y |
x = x & y |
| x ^= y |
x = x ^ y |
| x |= y |
x = x | y |
|
A comparison operator compare its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison, except for the === and !== operators. This generally results in a numerical comparison being performed. The following table describes the comparison operators.
|
| Operator |
Description |
Example |
| == |
is equal to |
5==8 returns false |
| === |
is equal to (checks for both value and type) |
x=5
y="5"
x==y returns true
x===y returns false |
| != |
is not equal |
5!=8 returns true |
| > |
is greater than |
5>8 returns false |
| < |
is less than |
5<8 returns true |
| >= |
is greater than or equal to |
5>=8 returns false |
| <= |
is less than or equal to |
5<=8 returns true |
|
Bitwise operators treat their operands as a set of 32 bits (zeros(0) and ones(1)), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard Java Script numerical values.
|
| Operator |
Usage |
Description |
| Bitwise AND |
a & b |
Returns a one in each bit position for which the corresponding bits of
both operands are ones. |
| Bitwise OR |
a | b |
Returns a one in each bit position for which the corresponding bits of
either or both operands are ones. |
| Bitwise XOR |
a ^ b |
Returns a one in each bit position for which the corresponding bits of
either but not both operands are ones. |
| Bitwise NOT |
~ a |
Inverts the bits of its operand. |
| Left shift |
a << b |
Shifts a in binary representation b bits to left, shifting in zeros from
the right. |
| Sign-propagating right shift |
a >> b |
Shifts a in binary representation b bits to right, discarding bits
shifted off. |
| Zero-fill right shift |
a >>> b |
Shifts a in binary representation b bits to the right, discarding bits
shifted off, and shifting in zeros from the left. |
|
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.
|
| Operator |
Description |
Example |
| && |
and |
x=6
y=3
(x < 10 && y > 1) returns true |
| || |
or |
x=6
y=3
(x==5 || y==5) returns false |
| ! |
not |
x=6
y=3
!(x==y) returns true |
|
In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example, "my " + "string" returns the string "my string".
The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring has the value "alpha," then the expression mystring += "bet" evaluates to "alphabet" and assigns this value to mystring.
|
txt1="What a very"
txt2="nice day!"
txt3=txt1+txt2
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords: javascript logical operators, comparison operators, assignment operators,
bitwise operators
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|