JavaScript operators are symbols or keywords used to perform operations on values or variables. They help manipulate data, compare values and control the logic of your code. Operators are a core part of JavaScript, enabling developers to build functional and dynamic applications.
Types of JavaScript Operators
JavaScript provides several types of operators, including:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Conditional (Ternary) Operators
- Type Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
– | Subtraction | 10 – 4 | 6 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 20 / 5 | 4 |
% | Modulus (Remainder) | 9 % 2 | 1 |
** | Exponentiation | 3 ** 2 | 9 |
Example:
let a = 10;
let b = 3;
console.log(a + b); // Output: 13
console.log(a % b); // Output: 1
2. Assignment Operators
Assignment operators assign values to variables.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and Assign | x += 2 | x = x + 2 |
-= | Subtract and Assign | x -= 3 | x = x – 3 |
*= | Multiply and Assign | x *= 4 | x = x * 4 |
/= | Divide and Assign | x /= 5 | x = x / 5 |
%= | Modulus and Assign | x %= 6 | x = x % 6 |
Example:
let x = 10;
x += 5; // Same as x = x + 5
console.log(x); // Output: 15
3. Comparison Operators
Comparison operators compare two values and return a Boolean (true
or false
).
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == “5” | true |
=== | Strict equal to | 5 === “5” | false |
!= | Not equal to | 5 != “5” | false |
!== | Strict not equal to | 5 !== “5” | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater than or equal to | 8 >= 8 | true |
<= | Less than or equal to | 9 <= 6 | false |
Example:
let age = 18;
console.log(age >= 18); // Output: true
4. Logical Operators
Logical operators perform logical operations and are often used with Boolean values.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
let hasLicense = true;
let hasCar = false;
console.log(hasLicense && hasCar); // Output: false
5. Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
Operator | Description | Example | Output |
---|---|---|---|
& | AND | 5 & 1 | 1 |
` | ` | OR | `5 |
^ | XOR | 5 ^ 1 | 4 |
~ | NOT | ~5 | -6 |
<< | Left shift | 5 << 1 | 10 |
>> | Right shift | 5 >> 1 | 2 |
6. String Operators
String operators are used to manipulate text.
Operator | Description | Example | Output |
---|---|---|---|
+ | Concatenation | “Hello ” + “World” | “Hello World” |
+= | Append | greeting += “World” | “Hello World” |
Example:
let text = "JavaScript ";
text += "is awesome!";
console.log(text); // Output: JavaScript is awesome!
7. Conditional (Ternary) Operator
The ternary operator simplifies conditional statements into a single line.
condition ? expressionIfTrue : expressionIfFalse;
Example:
let age = 18;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Output: Adult
8. Type Operators
Type operators are used to check or manipulate data types.
Operator | Description | Example | Output |
---|---|---|---|
typeof | Returns the data type | typeof “Hello” | “string” |
instanceof | Checks object instance | [] instanceof Array | true |