Operator
Overview
JavaScript operators are symbols that perform operations on variables and values. They are used to assign values, perform arithmetic operations, compare values, and more. Understanding and correctly using JavaScript operators is essential for writing efficient and effective JavaScript code.
Assignment Operators
Assignment operators are used to assign values to variables.
The most common assignment operator is the equals sign (=), which assigns the value on the right-hand side to the variable on the left-hand side.
let x = 5; // assigns the value 5 to the variable x
Other assignment operators combine an arithmetic operation with assignment. Some examples include:
- `+=`: adds the value on the right to the variable and assigns the result to the variable
- `-=`: subtracts the value on the right from the variable and assigns the result to the variable
- `*=`: multiplies the variable by the value on the right and assigns the result to the variable
- `/=`: divides the variable by the value on the right and assigns the result to the variable.
let x = 10;
x += 5; // equivalent to: x = x + 5; assigns the value 15 to the variable x
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.
Some commonly used arithmetic operators include:
- `+`: addition
- `-`: subtraction
- `*`: multiplication
- `/`: division
- `%`: modulus (returns the remainder of a division)
- `++`: increment (adds 1 to a variable)
- `--`: decrement (subtracts 1 from a variable)
let x = 5;
let y = 2;
console.log(x + y); // prints 7
console.log(x - y); // prints 3
console.log(x * y); // prints 10
console.log(x / y); // prints 2.5
console.log(x % y); // prints 1
let z = 3;
z++; // equivalent to: z = z + 1; z is now 4
z--; // equivalent to: z = z - 1; z is now 3
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (true or false) based on the comparison result.
Some commonly used comparison operators include:
- `==`: equal to
- `!=`: not equal to
- `>`: greater than
- `<`: less than
- `>=`: greater than or equal to
- `<=`: less than or equal to
- `===`: strict equal to (compares both value and type)
- `!==`: strict not equal to
let x = 5;
let y = 2;
console.log(x == y); // prints false
console.log(x != y); // prints true
console.log(x > y); // prints true
console.log(x < y); // prints false
console.log(x >= y); // prints true
console.log(x <= y); // prints false
console.log(x === '5'); // prints false
console.log(x !== '5'); // prints true
Logical Operators
Logical operators are used to combine multiple conditions and perform logical operations.
Some commonly used logical operators include:
- `&&`: logical AND (returns true if both conditions are true)
- `||` : logical OR (returns true if at least one condition is true)
- `!`: logical NOT (reverses the logical state of a condition)
let x = 5;
let y = 2;
console.log(x > 3 && y > 1); // prints true
console.log(x > 3 || y > 1); // prints true
console.log(!(x > 3)); // prints false
console.log(!(x < 3)); // prints true
Conditional (Ternary) Operator
The conditional (ternary) operator is a shorthand way of writing an if-else statement.
condition ? expression1 : expression2;
The condition is evaluated, and if it is true, expression1 is executed. If the condition is false, expression2 is executed.
let x = 5;
let result = x > 3 ? 'x is greater than 3' : 'x is not greater than 3';
console.log(result); // prints 'x is greater than 3'
Key Takeaways
- Assignment operators are used to assign values to variables.
- Arithmetic operators perform mathematical calculations.
- Comparison operators compare two values and return a Boolean result.
- Logical operators combine multiple conditions and perform logical operations.
- The conditional operator is a shorthand way of writing an if-else statement.
Comments
Post a Comment