Operators are essential components in JavaScript that allow you to perform various operations on variables and values. This guide covers different types of JavaScript operators, including arithmetic, assignment, comparison, logical, and more.
1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 6 / 3 | 2 |
% | Modulus (Remainder) | 5 % 3 | 2 |
++ | Increment | let a = 1; a++ | 2 |
-- | Decrement | let a = 1; a-- | 0 |
Example:
let a = 10;
let b = 5;
let sum = a + b; // sum is 15
let difference = a - b; // difference is 5
let product = a * b; // product is 50
let quotient = a / b; // quotient is 2
let remainder = a % b; // remainder is 0
2. Assignment Operators:
Assignment operators assign values to variables.
| Operator | Description | Example | Result |
|---|---|---|---|
= | Assignment | x = 5 | x = 5 |
+= | Addition Assignment | x += 5 | x = x + 5 |
-= | Subtraction Assignment | x -= 5 | x = x - 5 |
*= | Multiplication Assignment | x *= 5 | x = x * 5 |
/= | Division Assignment | x /= 5 | x = x / 5 |
%= | Modulus Assignment | x %= 5 | x = x % 5 |
Example:
let x = 10;
x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
x /= 4; // x is now 6
x %= 2; // x is now 0
3. Comparison Operators:
Comparison operators compare two values and return a boolean value (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
=== | Equal value and type | 5 === "5" | false |
!= | Not equal | 5 != 3 | true |
!== | Not equal value or type | 5 !== "5" | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 5 <= 3 | false |
Example:
let a = 10;
let b = 20;
console.log(a == b); // false
console.log(a != b); // true
console.log(a < b); // true
console.log(a > b); // false
console.log(a <= 10); // true
console.log(a === "10"); // false
4. Logical Operators:
Logical operators are used to combine multiple comparison operations.
| Operator | Description | Example | Result |
|---|---|---|---|
&& | Logical AND | true && false | false |
| ` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
let age = 25;
let hasLicense = true;
let canDrive = age >= 18 && hasLicense; // true
let isAdult = age >= 18;
let canVote = isAdult || hasLicense; // true
let isMinor = !isAdult; // false
5. String Operators:
String operators are used to concatenate (combine) strings.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Concatenation | "Hello" + " World" | "Hello World" |
+= | Concatenation Assignment | let text = "Hello"; text += " World"; | "Hello World" |
Example:
let greeting = "Hello, ";
let name = "Alice";
let welcomeMessage = greeting + name; // "Hello, Alice"
6. Conditional (Ternary) Operator:
The conditional (ternary) operator is a shorthand for the if...else statement.
Syntax:
condition ? expressionIfTrue : expressionIfFalse
Example:
let age = 20;
let canVote = (age >= 18) ? "Yes" : "No";
console.log(canVote); // "Yes"
7. Type Operators
Type operators are used to identify the type of a variable.
| Operator | Description | Example | Result |
|---|---|---|---|
typeof | Returns the type | typeof "John" | "string" |
instanceof | Returns true if an object is an instance of a class | obj instanceof Object | true |
Example:
let name = "Alice";
console.log(typeof name); // "string"
let date = new Date();
console.log(date instanceof Date); // true
Conclusion:
JavaScript operators are powerful tools that enable you to perform a wide range of operations, from arithmetic calculations and value assignments to comparisons and logical operations. Understanding how to use these operators effectively is essential for writing efficient and functional JavaScript code. By mastering these operators, you can manipulate data, make decisions, and create complex expressions with ease.