JavaScript Operators
JavaScript is a powerful and versatile programming language, and operators are an essential part of any JavaScript program. They help you perform operations on variables, values, and expressions. In this guide, we’ll take a detailed look at JavaScript operators, types, and provide practical examples for each one.
JavaScript operators are special symbols or keywords used to perform operations on values or variables. They can be categorized into several types, each serving a different purpose in JavaScript. The most common types of operators include:
Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 → 8 |
- |
Subtraction | 5 - 3 → 2 |
* |
Multiplication | 5 * 3 → 15 |
/ |
Division | 6 / 3 → 2 |
% |
Modulus (Remainder) | 7 % 3 → 1 |
** |
Exponentiation | 2 ** 3 → 8 |
let num1 = 10;
let num2 = 5;
console.log(num1 + num2); // Output: 15
console.log(num1 - num2); // Output: 5
console.log(num1 * num2); // Output: 50
console.log(num1 / num2); // Output: 2
console.log(num1 % num2); // Output: 0
console.log(num1 ** num2); // Output: 100000
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Simple Assignment | x = 10 |
+= |
Add and Assign | x += 5 → x = x + 5 |
-= |
Subtract and Assign | x -= 5 → x = x - 5 |
*= |
Multiply and Assign | x *= 5 → x = x * 5 |
/= |
Divide and Assign | x /= 5 → x = x / 5 |
%= |
Modulo and Assign | x %= 5 → x = x % 5 |
let x = 10;
x += 5; // x = x + 5
console.log(x); // Output: 15
x *= 2; // x = x * 2
console.log(x); // Output: 30
Comparison operators are used to compare two values. They return true
or false
based on the condition.
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 → true |
=== |
Strict Equal to | 5 === "5" → false |
!= |
Not equal to | 5 != 3 → true |
!== |
Strict Not Equal to | 5 !== "5" → true |
> |
Greater than | 5 > 3 → true |
< |
Less than | 3 < 5 → true |
>= |
Greater than or equal | 5 >= 3 → true |
<= |
Less than or equal | 3 <= 5 → true |
let a = 10;
let b = 20;
console.log(a == b); // Output: false
console.log(a !== b); // Output: true
console.log(a > b); // Output: false
console.log(a <= b); // Output: true
Logical operators are used to combine conditional statements. They are essential for controlling program flow.
Operator | Description | Example |
---|---|---|
&& |
Logical AND | true && false → false |
` | ` | |
! |
Logical NOT | !true → false |
let x = true;
let y = false;
console.log(x && y); // Output: false
console.log(x || y); // Output: true
console.log(!x); // Output: false
Bitwise operators work with the binary representation of numbers.
Operator | Description | Example |
---|---|---|
& |
Bitwise AND | 5 & 3 → 1 |
` | ` | Bitwise OR |
^ |
Bitwise XOR | 5 ^ 3 → 6 |
~ |
Bitwise NOT | ~5 → -6 |
<< |
Left Shift | 5 << 1 → 10 |
>> |
Right Shift | 5 >> 1 → 2 |
let num1 = 5; // 0101 in binary
let num2 = 3; // 0011 in binary
console.log(num1 & num2); // Output: 1 (0001 in binary)
console.log(num1 | num2); // Output: 7 (0111 in binary)
console.log(num1 ^ num2); // Output: 6 (0110 in binary)
The ternary operator is a shorthand for if-else
statements. It has three parts: a condition, a true value, and a false value.
condition ? expression1 : expression2;
let age = 18;
let canVote = (age >= 18) ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote); // Output: Yes, you can vote.
Unary operators perform operations on a single operand.
Operator | Description | Example |
---|---|---|
++ |
Increment | x++ |
-- |
Decrement | x-- |
+ |
Unary Plus | +x |
- |
Unary Negation | -x |
! |
Logical NOT | !x |
let num = 5;
console.log(++num); // Output: 6
console.log(num--); // Output: 6 (then num becomes 5)