JavaScript Operators
Written By: Avinash Malhotra
Updated on
Operators
JavaScript operators are used to perform tasks like assignment, addition, subtraction, and comparisons. JavaScript provides arithmetic, logical, assignment, and comparison operators.
JavaScript also supports binary and unary operators, as well as the ternary (conditional) operator.
Type of Operators in Javascript
Binary operator
Binary operators require two operands, one before and one after the operator. x + y = z
x (operand) +(operator) y(operand)
Unary Operator
Unary operators require only one operand, either before or after the operator. i++
x++
++x
Ternary Operator
Ternary Operator is the conditional operator in JavaScript which uses three operands. For example, (3>2) ? console.log("yes") : console.log("no") will print yes, while (1>2) ? console.log("yes") : console.log("no") will print no.
(3>2) ? console.log("yes") : console.log("no");
(1>2) ? console.log("yes") : console.log("no");
Arithmetic Operators
An Arithmetic operator is used to perform Arithmetic operations between values. Like Addition, Subtraction, Multiplication, Division etc
Arithmetic operators in JavaScript
| Operator | Description | Example |
|---|---|---|
| + | Addition, can also concat | 2+3=5 "2" + 3 = "23" |
| - | Subtraction | 5-3=2 "5" - 3 = 2 |
| * | Multiply | 2*3=6 |
| / | Divide | 6/3=2 |
| % | Remainder | 6%3=0 |
| ++ | Increment, y++ means y = y+1 | var y=2; ++y; y=3 |
| -- | Decrement, y-- means y = y-1 | var y=2; --y; y=1 |
| ** | Exponentiation Operator | 2**3 returns 8 |
The ** Exponentiation Operator was introduced in JS ES7. There is no support for Exponentiation Operator in IE browser, chrome<52 and Firefox<52
Logical Operators
Logical Operators are used to check logic between two operators. and (&&), or (||) and not (!) are logical operators.
Logical Operators in JavaScript
| Operator | Description | Example |
|---|---|---|
| && | and, when both are correct | 2 < 5 && 2> 1 is true |
| || | or, when any one is correct | var x=2, 2>5 || x==2 is true |
| ! | not | !(2==3) is true |
Assignment Operators
Assignment operators are used to assign values to JavaScript variables. Examples include =, +=, -=, *=, and /=.
Assignment Operators in JavaScript
| Operator | Description | Example |
|---|---|---|
| = | Assignment | x=2; means x is 2 |
| += | Addition Assignment | let x=2; x+=2 means x=x+2 |
| -= | Subtraction Assignment | let x=2; x-=2 means x=x-2 |
| *= | Multiplication Assignment | let x=2; x*=2 means x=x*2 |
| /= | Division Assignment | let x=2; x/=2 means x=x/2 |
| ?? | Nullish coalescing | null ?? 0 return 0 undefined ?? 0 return 0 |
The ?? Nullish coalescing was introduced in JS ES 2021.
Comparison Operators
Comparison operators are used in a statement to compare two values. Like == is used to compare values but === is used to compare values and datatypes.
Comparison Operators in JavaScript
| Operator | Description | Example |
|---|---|---|
| == | Equal to, check value only |
2 == "2" is true true == 1 is true undefined == null is true |
| === | Strict equal to, check value and data type both | 2==="2" is false true === 1 is false undefined === null is false |
| != | not equal | 2!=1 is true |
| !== | not strict equal | 2!=="2" is true |
| > | greater than | 2> 5 is false, & 2 <5 is true |
| >= | greater than or equal to | 3>=3 is true |
| < | less than | 1< 3 is true |
| <= | less than or equal to | 2<=2 is true |
Key takeaways
- Prefer
===(strict equality) to avoid unexpected type coercion from==. - Use parentheses to make complex expressions clearer and avoid operator precedence bugs.
- Check browser compatibility for newer operators like
**and??.
See also
Related topics: JavaScript Variables, JavaScript Functions, Arrow Functions