Page Content
JavaScript’s Operators
Operators are used for JavaScript’s arithmetic, comparison, logical, assignment expressions and more. Most operators are represented by punctuation characters such as + and =, some, however, are represented by keyword such as delete and instanceof.
Table below is organized by operator precedence.
- The operator listed first has higher precedence then those listed last.
- Operators separated by a horizontal line have different precedence levels.
- The column labeled A gives the operator associativity, which can be L (left to right) or R (right to left).
- Column N specifies the number of operands.
- The column labeled Types lists the expected types of the operands and (after the → symbol) the result type for operator.
Operator |
Operation |
A |
N |
Types |
++ |
Pre- or post-decrement |
R |
1 |
Ival→num |
— |
Pre- or post-decrement |
R |
1 |
Ival→num |
– |
Negative number |
R |
1 |
num→num |
+ |
Convert to number |
R |
1 |
num→num |
~ |
Invert bits |
R |
1 |
int→int |
! |
Invert boolean value |
R |
1 |
bool→bool |
delete |
Remove a property |
R |
1 |
Ival→bool |
typeof |
Determine type of operand |
R |
1 |
any→str |
void |
Return undefined value |
R |
1 |
any→undef |
*,/,% |
Multiply, divide, remainder |
L |
2 |
num,num→num |
+,- |
Add, subtract |
L |
2 |
num,num→num |
+ |
Concatenate strings |
L |
2 |
str,str→str |
<< |
Shift left |
L |
2 |
int,int→int |
>> |
Shift right with sign extension |
L |
2 |
int,int→int |
>>> |
Shift right with zero extension |
L |
2 |
int,int→int |
<,<=,>,>= |
Compare in numeric order |
L |
2 |
num,num→bool |
<,<=,>,>= |
Compare in alphabetic order |
L |
2 |
str,str→bool |
instanceof |
Test object class |
L |
2 |
obj,func→bool |
in |
Test whether property exists |
L |
2 |
str,obj→bool |
== |
Test for equality |
L |
2 |
any,any→bool |
!= |
Test for inequality |
L |
2 |
any,any→bool |
=== |
Test for strict equality |
L |
2 |
any,any→bool |
!== |
Test for strict inequality |
L |
2 |
any,any→bool |
& |
Compute bitwise AND |
L |
2 |
int,int→int |
^ |
Compute bitwise XOR |
L |
2 |
int,int→int |
| |
Compute bitwise OR |
L |
2 |
int,int→int |
&& |
Compute logical AND |
L |
2 |
any,any→any |
|| |
Compute logical OR |
L |
2 |
any,any→any |
?: |
Choose 2^{nd }and 3^{rd }operand |
R |
3 |
bool,any,any→any |
= |
Assign to a variable or property |
R |
2 |
Ival,any→any |
*=,/=,%=,+=,
-=,&=,^=,|=, <<=,>>=,>>>= |
Operate and assign |
R |
2 |
Ival,any→any |
, |
Discard 1^{st} operand, return 2^{nd} |
L |
2 |
any,any→any |
lval :Notice that the assignment operators and a few of other operators listed in table above expect an operand of type lval. lavlue is a historical term that means “an expression that can legally appear on the left side of an assignment expressions.”
In JavaScript, variables, properties of objects, and elements of arrays are lvalues. The ECMAScript specification allows built-in functions to return lvalues but does not define any function that behaves that way.
Order of Evaluation
Operator precedence and associativity specify the order in which operations are performed in a complex expression, but they do not specify the order in which the sub-expressions are evaluated. JavaScript always evaluates expression in strictly left-to-right order. In the expression w=x+y*z, for example, the sub-expression w is evaluated first, followed by x, y and z. Then the values of y and z are multiplied, added to the value of x and assigned to the variable or property specified by expression w. Adding parentheses to the expression can change the relative order of multiplication, addition and assignment, but not the left-to-right order of evaluation.
Order of evaluation only makes a difference if any of the expressions being evaluated has side-effects that affect the value of another expression. If expression x increments a variable that is used by expression z, then the fact that x is evaluated before z in important.
Arithmetic Operators
Arithmetic operations can be carried out by these arithmetic operators between variables or values. Taking value of b as 10 below are the arithmetic operators explained with example.
Operator |
Operation |
Example |
Result |
+ |
Addition |
a = b+7 |
a = 17, b = 10 |
– |
Subtraction |
a = b – 7 |
a = 3, b = 10 |
* |
Multiplication |
a = b * 7 |
a = 70, b = 10 |
/ |
Division |
a = b / 3 |
a = 3.3333333333333335, b = 10 |
% |
Modulus (remainder after division) |
a = b % 7 |
a = 3, b = 10 |
++ |
Increment |
a = ++b a = b++ |
a = 11, b = 11 a = 10, b = 11 |
— |
Decrement |
a = –b a = b– |
a = 9, b = 9 a = 10, b = 9 |
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html charset=utf-8" /> <title>JavaScript Arithmetic Operator</title> </head> <body> <h2>This is an example of JavaScript Arithmetic Operator</h2> <p>b = 10, then calculate a = b % 7.</p> <button onclick="showResult()">Rssult</button> <p id="result_here"> <font color="green">result here</font> </p> <script> functionshowResult() { var b = 10; var a = b % 7; document.getElementById("result_here").innerHTML = "a = " + a + ", b = " + b; } </script> </body> </html>
Assignment Operators
As indicated by the name, assignment is done by these operators. Taking a = 4, b = 3.
Operator |
Example |
Result |
= |
a = b → a = b |
a = 3 |
+= |
a += b → a = a + b |
a = 7 |
-+ |
a -= b → a = a – b |
a = 1 |
*= |
a *= b → a = a * b |
a = 12 |
/= |
a /= b → a = a / b |
a = 1.3333333333333333 |
%= |
a %= b → a = a % b |
a = 1 |
String Operator
The ‘+’ operator can be used to concatenate strings. Like this :
string1 = "I am learning "; string2 = "JavaScript here."; string3 = string1 + string2; // I am learning JavaScript here.
Bitwise Operators
The bitwise operators perform low-level manipulation of the bits in the binary representation of numbers. They expect integer operands and behave as if those values were represented as 32-bit integers rather than 64-bit floating point values.
Operator |
Operation |
Example |
Result |
& |
AND |
a = 5& 1 → 0101 & 0001 |
0001 → 1 |
| |
OR |
a = 5 | 1 → 0101 | 0001 |
0101 → 5 |
~ |
NOT |
a = ~ 5 → 0101 |
-6 |
^ |
XOR |
a = 5 ^ 1 → 0101 ^ 0001 |
0100 → 4 |
<< |
Left Shift |
a = 5 << 1 → 0101 << 0001 |
1010 → 10 |
>> |
Right Shift |
a = 5 >> 1 → 0101 >> 0001 |
0010 → 2 |
Comaprion Operators
Comparison operator tests equality or difference between variables or values. Taking a = 7:
Operator |
Operation |
Example |
Result |
== |
equal to |
a == 9 a == 7 |
false true |
=== |
equal value and equal type |
a === “7” a === 7 |
false true |
!= |
not equal |
a != 9 |
true |
!== |
not equal value or not equal type |
a !== “7” a !== 7 |
true false |
> |
greater than |
a > 9 |
false |
< |
less than |
a < 9 |
true |
>= |
greater than or equal to |
a >=9 |
false |
<= |
less than or equal to |
a <=9 |
true |
Logical Operators
Logic between variables or values is tested by logical operators. Taking a = 3 and b = 7:
Operator |
Operation |
Example |
Result |
&& |
and |
( a < 5 && b > 6 ) |
true |
|| |
or |
( a == 5 || b == 6 ) |
false |
! |
not |
!( a == b ) |
true |
Conditional Operators
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
result = ( marks > 60 )? "Pass" : "Fail";
typeof Operators
The typeof operator returns the type of a variable (or an expression):
var x = 3;var y = "Name"; typeof x // Returns number typeof y // Returns string typeof 3.14 // Returns number typeof false // Returns boolean typeof [1,2,3,4] // Returns object
Delete Operators
The delete operator can be used to delete properties from objects:
var person = {firstName:"Ramesh", lastName:"Singh", age:32, eyeColor:"brown"}; delete person.age;
can you help me i not understad decrement.