JavaScript Operators
JavaScript supports various types of operators, which are symbols used to perform operations on operands (variables, values, or expressions).
Here's an overview of the different types of operators in JavaScript:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- String Operators
- Logical Operators
- Bitwise Operators
- Ternary Operators
- Type Operators
JavaScript Arithmetic Operators
Used to perform arithmetic operations on numeric operands.
Example
let a = 3; let x = (100 + 50) * a;
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplications |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
** | Exponentiation |
Assignment Operators
Used to assign values to variables.
Example
let a = 10; let b = 5; a += b; // Equivalent to a = a + b;
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x+=y | x = x + y |
-= | x-=y | x = x - y |
*= | *= | x = x * y |
/= | /= | x = x / y |
%= | %= | x = x % y |
**= | x**=y | x = x ** y |
Comparison Operators:
Used to compare two values and return a Boolean result.
Example
let c = 10; let d = 5; console.log(c > d); // Greater than (>) console.log(c < d); // Less than (<) console.log(c >= d); // Greater than or equal to (>=) console.log(c <= d); // Less than or equal to (<=) console.log(c === d); // Equal to (strict equality) (===) console.log(c !== d); // Not equal to (!==)
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? | ternary operator |
Logical Operators:
Used to perform logical operations.
Example
let e = true; let f = false; console.log(e && f); // Logical AND (&&) console.log(e || f); // Logical OR (||) console.log(!e); // Logical NOT (!)
Operator | Description |
---|---|
&& | logical and |
|| | logical or |
! | logical not |
Bitwise Operators:
Used to perform bitwise operations on operands.
Example
let g = 5; // 101 (in binary) let h = 3; // 011 (in binary) console.log(g & h); // Bitwise AND (&) console.log(g | h); // Bitwise OR (|) console.log(~g); // Bitwise NOT (~) console.log(g ^ h); // Bitwise XOR (^) console.log(g << 1); // Bitwise Left Shift (<<) console.log(g >> 1); // Bitwise Right Shift (>>)
Operator | Description |
---|---|
& | AND |
| | OR |
~ | NOT |
^ | XOR |
<< | left shift |
>> | right shift |
>>> | unsigned right shift |
String Operators
Used to concatenate strings.
Example
let str1 = "Hello"; let str2 = "World"; let greeting = str1 + " " + str2; // Concatenation (+)
Unary Operators:
Unary Operators: