javascript operators
JavaScript Operators
An operator is a special kind of symbol which performs a particular task.
Example: 5+6 =6. In this expression 5 and 6 are operands and + is an operator.
Arithmetic Operators
Assume a=20 and b=5 then:
S.No | Operator and Description |
---|---|
1 | + (Addition)
a+b=25 |
2 | -(subtraction) a-b=15; |
3 | * (multiplication) a*b=100 |
4 | / (division) a/b= 4 |
5 | % (modulus) outputs the remainder of an integer division a%b=0 |
6 | ++ (increment) a++=21 |
7 | --(decrement) a--=19 |
Assume a=20, b=5 then:
S.No | Operator and Description |
---|---|
1 | ==(Equal) checks if the values of two operands are equal or not, if yes, then condition becomes true. (a==b) is not true. |
2 | !=(not equal) checks if the values of two operands are equal or not, if values are not equal, then condition becomes true. (a!=b) is true. |
3 | >(greater than) Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true. (a>b) is true. |
4 | <(less than) Checks the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true. a<b is not true. |
5 | >= (greater than or equal to) Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true. a>=b is true. |
6 | <= (less than or equal to) Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true. a<=b is false. |
Logical Operators
S.No | Operator and Description |
---|---|
1 | &&(Logical And) if both operands are non-zero, then the condition becomes true. a&&b is true. |
2 | !!(logical OR) if any one of two operands are non-zero, then the condition becomes true. (a!!b) is true. |
3 | ! (logical Not) Reverses the logical state of its operand. If a condition is true, then the logical NOT operator will make it false. !(a&&b) is false. |
Bitwise Operators:
S.No | Operator and Description |
---|---|
1 | & (Bitwise AND) Returns a one in each bit position for which the corresponding bits of both operands are ones. |
2 | !(Bitwise OR) Returns a one in each bit position for which the corresponding bits or either or both operands are ones. |
3 | ^(Bitwise XOR) Returns a one in each bit position for which the corresponding bits of either but not both operands are ones. |
4 | ~(Bitwise NOT) Inverts the bits of its operand. |
5 | <<(Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. |
6 | >>(Right Shift) It moves all the bits in its first operand to the right by the number of places specified in the second operand. New bits are filled with zeros.
The >> operator shifts the bits of expression1
right by the number of bits specified in expression2. The sign bit of
expression1 is used to fill the digits from the left. Digits shifted off the
right are discarded. For example, after the following code is evaluated, temp
has a value of -4: -14 (11110010 in two's complement binary) shifted right two
bits equals -4 (11111100 in two's complement binary).
Expression:
var temp
temp = -14 >> 2
|
7 | >>>(Right Shift with zero) (also known as Unsigned Right Shift)
The >>> operator shifts the bits of first operand right by the number of bits specified
in second operand. Zeroes
are filled in from the left. Digits shifted off the right are discarded. For
example:
var temp
temp= -14>>>2
The variable temp has
an initial value -14 (11111111 11111111 11111111 11110010 in two's complement
binary). When it is shifted right two bits, the value equals 1073741820
(00111111 11111111 11111111 11111100 in binary).
|
Assignment Operators
S.No | Operator and Description |
---|---|
1 | =(Simple Assignment) Assigns values from right side operand to left side operand Ex: c=a+b will assign the value of a+B into c. |
2 | +=(Add and Assignment) It adds the right operand to the left operand and assigns result to the left operand. Ex: c+=a is equivalent to c=c+a. |
3 | -=
(Subtract and Assignment)
It
subtracts the right operand from the left operand and assigns the result to the
left operand.
Ex: C -= A is equivalent to C = C - A
|
4 | *=
(Multiply and Assignment)
It
multiplies the right operand with the left operand and assigns the result to
the left operand.
Ex: C *= A is equivalent to C = C * A
|
5 | /=
(Divide and Assignment)
It
divides the left operand with the right operand and assigns the result to the
left operand.
Ex:
C /= A is equivalent to C = C / A
|
6 | %=
(Modules and Assignment)
It takes
modulus using two operands and assigns the result to the left operand.
Ex: C %= A is equivalent to C = C % A
|
Note: Same logic applies to Bitwise
operators, so they will become <<=, >>=, >>=, &=, |= and
^=.
Miscellaneous Operators
S.No | Operator and Description |
---|---|
1 | delete Operator Deletes property from an object or removes an element from an array. Syntax: delete expression |
2 | typeof Operator Returns a string identifies the datatype of an expression. Syntax: typeof[(]expression[)] |
3 | void Operator
Prevents an expression from returning a value.
Syntax:
void expression
|
4 | instanceof Operator
Returns a Boolean value that indicates whether or
not an object is an instance of a particular class.
Syntax:
result= object instance of class
The instanceof operator returns true if object is
an instance of class. It returns true if true if class is present in the
object's prototype chain. It returns false if object is not an instance of
class, or if object is null.
|
5 |
new Operator
Creates new object.
Syntax: new constructor ([arguments])
|
6 | in Operator Tests for the existence of a property in an object. Syntax: result= property in object |
7 | Conditional
Operator (? :)
The conditional operator first evaluates an
expression for a true or false value and then executes one of the two given
statements depending upon the result of the evaluation.
? : (Conditional )
If Condition is true? Then value X : Otherwise value Y
|
Subscribe to:
Post Comments
(
Atom
)
great
ReplyDelete