JavaScript Operators

Ratings:
(4)
Views: 417
Banner-Img
Share this blog:

 

 What is the operator?

A simple answer can be given using the expression 4+5 is equal to 9, Here 4 and 5 are called operands, and + is called operator JavaScript language supports the following type of operators. Arithmetic Operators Comparison Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators Lets have a look at all operators one by one.   

Arithmetic Operators

There are following arithmetic operators supported by JavaScript language Assume variable A holds 10 and variable B holds 20 then

Operator Description Example
+ Adds 2 operands A+B will give 30
- Subtract second opened from the first A-B will give -10
* Multiply both operands A*B will give 200
/ Divide numerator by denumerator B/A will give 2
% Modulus operator and a reminder of after an integer division B%A will give 0
++ The increment operator increases integer value by one A++ will give 11
-- The decrement operator decreases integer value by one A—will give 9

  Note:  Addition operator (+) works for number as well as strings Ex: “a” +10 will give “a10”

Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online " JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.

 

  The comparison operators There are following comparison operators supported by javascript language Assume variable A holds 10 and variable B holds 20 then

Operator Description Example
== Checks if the value of 2 operands are equal or not, if yes then the condition becomes true (A==B) is not true
!= Checks if the value of 2 operands are equal or not, if values are not equal then the condition becomes true (A!=B) is true
> 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 not true
< 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
>= 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 not true
<= 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 true
     

 

Logical Operators

There are following logical operators supported by javascript language Assume variable A holds 10 and variable B holds 20 then

operator description Example
&& Called logical AND operator, if both the operands are non zero then the condition becomes true (A&&B) is true
|| Called logical OR operator, if any of the 2 operands are non zero then the condition becomes true (A||B) is true
! Called Logical NOT Operator. Use to reverses the logical state of its operand. if a condition is true logical NOT operator will make false !(A&&B) is false

 

Bitwise Operators

There are following logical operators supported by javascript language Assume variable A holds 10 and variable B holds 20 then

Operator description Example
& Called a bitwise operator. It performs a Boolean AND operation on each bit of its integer arguments (A&B) is 2
| Called a bitwise operator. It performs a Boolean OR operation on each bit of its integer arguments (A|B) is 3
^ Called bitwise XOR operator. It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand 2 is true, but not both (A^B) is 1
~ Called bitwise NOT operator. It is a unary operator and operates by reversing all bits in the operand (~B) is 4
<< Called bitwise shift left operator. It moves all bits in its 1st operand to the left by the number of places specified in the second operand. New bits are filled with 0 shifting a value left by one position is equivalent to multiplying by 2, shifting 2 positions is equivalent to multiply by 4, etc.. (A<<1)is 4
>> Called bitwise shift right with sign operator. It moves all bits in its 1st operand to the right by the no.of places specified in the 2nd operand. The bits filled in on the left depending on the sign bit of the original operand, in order to preserve the sign of the result. If the 1st operand is positive, the result has ones placed in the high bits. Shifting a value right one place is equalling to dividing by 2, shifting right 2 places is equivalent to integer division by 4, and so on. (A>>1) is 1
>>> Called Bitwise shift right with zero operators. This operator is just like the >> operator, except that the bits shifted in on the left are always zero (A>>>1) is 1

 

Assignment  Operators

There are following arithmetic operators supported by JavaScript language Assume variable A holds 10 and variable B holds 20 then  

Operator    Description Example
= Simple assignment operator, Assign values from right side operands to left side operand C=A+B will assign the value of A+B into C
+= Add AND assignment operator, it adds the right operand to the left operand and assigns the result to the left operand C+=A is equivalent to C = C+A
-= Subtract AND assignment operator, it subtracts the right operand from the left operand and assigns the result to the left operand C-=A is equivalent to C=c-A
*= Multiply AND assignment operator, It multiplies the right operand with the left operand and assigns the result to the left operand C*=A is equivalent to C=C*A
/= Divide AND assignment operator, It divides the left operand with the right operand and assigns the result to the left operand C/=A is equivalent to C=C/A
%= Modulus AND assignment operator, it takes modulus using two operands and assign the result to the left operand C%=A is equivalent to C=C%A

  Note: Same logic applies to the Bitwise operator so they will become like <<=,>>=,>>=&=!= and ^=  

Miscellaneous Operator The Conditional Operator (? :)

There is an operator called a conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax:  

Operator Description Example
?= Conditional Expression If the condition is true? Then value X: Otherwise value Y

  The typeof Operator The typeof is a unary operator that is placed before its single operand, which can be of any type. its value is a string indicating the data type of the operand. The typeof operator evaluates to “number” , “string”, or “boolean” if its operand is a number, string, or boolean value and returns true or false based on the evaluation   Here is the list of return values for the typeof Operator:

    Type string returned by typeof
Number “number”
String “string”
Boolean “boolean”
object “object”
Function “function”
Undefined “undefined”
Null “object”

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.