JavaScript Operators:
JavaScript operators are symbols used to perform operations on variables and values. They allow you to manipulate data and perform calculations.
Here are the main types of operators in JavaScript:
1. Arithmetic Operators:
- `+` (Addition): Adds two operands.
- `-` (Subtraction): Subtracts the right operand from the left operand.
- `*` (Multiplication): Multiplies two operands.
- `/` (Division): Divides the left operand by the right operand.
- `%` (Modulus): Returns the remainder when the left operand is divided by the right operand.
- `` (Exponentiation): Raises the left operand to the power of the right operand.
javascript
let a = 5;
let b = 2;
let result = a + b; // result is 7
JavaScript Operators |
2. Assignment Operators:
- `=` (Assignment): Assigns the value of the right operand to the left operand.
- `+=`, `-=`, `*=`, `/=`, `%=` (Compound Assignment): Performs the corresponding arithmetic operation and assigns the result to the left operand.
javascript
let x = 10;
x += 5; // x is now 15
3. Comparison Operators:
- `==` (Equality): Returns true if the operands are equal.
- `===` (Strict Equality): Returns true if the operands are equal in value and type.
- `!=` (Inequality): Returns true if the operands are not equal.
- `!==` (Strict Inequality): Returns true if the operands are not equal in value or type.
- `>` (Greater Than), `<` (Less Than), `>=` (Greater Than or Equal To), `<=` (Less Than or Equal To): Compare the values of the operands.
javascript
let a = 5;
let b = '5';
console.log(a == b); // true (loose equality)
console.log(a === b); // false (strict equality)
4. Logical Operators:
- `&&` (Logical AND): Returns true if both operands are true.
- `||` (Logical OR): Returns true if at least one of the operands is true.
- `!` (Logical NOT): Returns true if the operand is false and vice versa.
javascript
let x = true;
let y = false;
console.log(x && y); // false
console.log(x || y); // true
console.log(!x); // false
5. Unary Operators:
- `+` (Unary Plus): Converts its operand to a number.
- `-` (Unary Minus): Negates its operand.
javascript
let num = '5';
console.log(+num); // 5 (converted to a number)
6. Conditional (Ternary) Operator:
- `condition ? expr1 : expr2`: Returns `expr1` if `condition` is true; otherwise, it returns `expr2`.
javascript
let age = 20;
let status = (age >= 18) ? 'Adult' : 'Minor';
7. Bitwise Operators:
- `&` (Bitwise AND): Performs a bitwise AND operation.
- `|` (Bitwise OR): Performs a bitwise OR operation.
- `^` (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.
- `~` (Bitwise NOT): Inverts the bits of its operand.
- `<<` (Left Shift): Shifts the bits of the left operand to the left.
- `>>` (Right Shift): Shifts the bits of the left operand to the right.
- `>>>` (Unsigned Right Shift): Shifts the bits of the left operand to the right, filling with zero.
javascript
let a = 5; // binary representation: 0101
let b = 3; // binary representation: 0011
console.log(a & b); // 1 (bitwise AND)
console.log(a | b); // 7 (bitwise OR)
console.log(a ^ b); // 6 (bitwise XOR)
console.log(~a); // -6 (bitwise NOT)
console.log(a << 1); // 10 (left shift by 1)
console.log(a >> 1); // 2 (right shift by 1)
These are the fundamental operators in JavaScript that allow you to perform a wide range of operations on variables and values. Understanding how to use these operators is crucial for writing effective and expressive JavaScript code.