JavaScript Operators
- JavaScript operators are symbols used to perform operations on values and variables
- Operators are used for calculations, comparisons, assigning values, and logical decisions
- JavaScript provides different types of operators for different tasks
- Common operators include arithmetic, assignment, comparison, and logical operators
What are JavaScript Operators?
JavaScript operators are special symbols or keywords that perform operations on one or more values. They help developers perform calculations, compare values, assign data, and control program logic.
For example, the + operator is used to add numbers,
while the == operator is used to compare two values.
Types of JavaScript Operators
JavaScript provides different types of operators. The main types are:
Used to perform mathematical calculations.
Used to assign values to variables.
Used to compare two values.
Used to combine multiple conditions.
1. JavaScript Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.
let a = 10;
let b = 5;
console.log (a+ b);
console.log (a - b);
console.log (a * b);
console.log (a / b);
5
50
2
Arithmetic Operators List
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
2. JavaScript Assignment Operators
Assignment operators are used to assign values to variables. The equal sign (=) is the most common assignment operator.
let x = 10;
x += 5;
console.log(x);
3. JavaScript Comparison Operators
Comparison operators compare two values and return a boolean result: true or false.
let age = 18;
console.log(age >= 18);
4. JavaScript Logical Operators
Logical operators are used to combine multiple conditions.
let age = 20;
if(age > 18 && age < 30) {
console.log("Eligible");
}