An Operator is a symbol or word that is reserved for a special task or action. Every operator works on one or more operands, that is, an operator can take the values of more than one operand. After taking the values, it performs an action on the operands and returns the result of that action.

do you know  what are the reserved keywords inn javascript? click here to know!

In JavaScript, there is whole gamut of operators that you can use as per your requirements. Some of the operators work on a single operand, while some work on two or more operands.

Moreover, some operators work on numbers, while others work on strings and Boolean values.

Operators Available in JavaScript

 

Arithmetic Operators
Operator Description Example
 +  Adds two numbers or joins two strings. This operator also represents a positive number when it is prefixed to a number  50 + 10 returns 60

"My" + "Name" returns "My Name"

 –  Subtracts two numbers or represents a negative number.  -50 + 10 returns -40
 *  Multiplies two numbers  50 * 10 returns 500
 /  Divides two numbers and returns the quotient  50 / 10 returns 5
 %  Divides two numbers and returns the remainder  55 % 10 returns 5
 ++  Increments the value of a number by one. It can be prefixed or suffixed to a number. When prefixed, the value is incremented in the current statement, and when suffixed, the value is decremented after the current statementd  myVar1 = 45

myVar2 = ++myVar1 assigns 46 to myVar2

myVar2 = myVar1++ assigns 45 to myVar2

 

 —  Decrements the value of a number by one. It can be prefixed or suffixed to a number. When prefixed, the value is decremented in the current statement, and when suffixed, the value is decremented after the current statement. myVar1 = 45

myVar2 = --myVar1 assigns 44 to myVar2

myVar2 = myVar1-- assigns 45 to myVar2

 

 

Assignment Operators
Operator Description Example
 =  Assigns the value to the left hand side variable.  myVar = 90
 +=  Adds the right hand side operand to the left hand side operand and assigns the result to the left hand side operand.  myVar1=45, myVar2=10

myVar1+=myVar2 assigns 55 to myVar1

 -=  Subtracts the right hand side operand from the left hand side operand and assigns the result to the left hand side operand. myVar1=45, myVar2=10

myVar1-=myVar2 assigns 35 to myVar1

 *=  Multiplies the right hand side operand with the left hand side operand and assigns the result to the left hand side operand. myVar1=45, myVar2=10

myVar1*=myVar2 assigns 450 to myVar1

 /=  Divides the left hand side operand by the right hand side operand and assigns the quotient to left hand side operand. myVar1=45, myVar2=10

myVar1/=myVar2 assigns 4.5 to myVar1

 %=  Divides the left hand side operand by the right hand side operand and assigns the remainder to the left hand side operand. myVar1=45, myVar2=10

myVar1%=myVar2 assigns 5 to myVar1

 

 

Comparison Operators
Operator Description Example
 ==  Returns true if both the operands are equal; otherwise, it returns false.  45==10 returns false
 !=  Returns true if both the operands are not equal; otherwise, it returns false.  45 != 10 return true
 >  Returns true if the left hand side operand is greater than the right hand side operand; otherwise, it returns false 45 > 10 returns true
 >=  Returns true if the left hand side operand is greater than or equal to the right hand side operand; otherwise, it returns false. 45 >= 10 returns true

 

 <  Returns true if the left hand side operand is less than the right hand side operand; otherwise, it returns false. 45 < 10 returns true
 <=  Returns true if the left hand side operand is less than or equal to the right hand side operand; otherwise, it returns false. 45 <= 10 returns false

 

Logical Operators
Operator Description Example
&&  Returns true only if both the operands are true; otherwise, it returns false.  true && true returns false
 ||  Returns true only if either of the operands is true. It returns false when both the operands are false.  true || false returns true
 !  Negates the operand, that is, returns true if the operand is false and returns false if the operand is true. !true returns false.
 ?: Returns the second operand if the first operand is true. However, if the first operand is false, it returns the third operand. myVar1=45, myVar2=10

myResult=(myVar1<myVar2)?myVar1:myVar2

returns 10.

learn the programming fundamentals of javascript!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.