
JavaScript Tutorial – Day 4: Operators in JavaScript
Operators in JavaScript are special symbols that allow you to perform operations on values and data types. In this JavaScript tutorial, weโll explore different types of operators such as arithmetic, assignment, comparison, and logical operatorsโwith clear examples you can practice right away.
What are Operators in JavaScript?
In simple terms, operators in JavaScript are used to manipulate values. They take one or more values (called operands) and return a result.
For example:
let x = 10;
let y = 5;
console.log(x + y); // 15
Here, the +
operator adds two values.
Arithmetic Operators in JavaScript
The arithmetic operators in JavaScript are used to perform basic math.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
๐ Example:
let a = 20;
let b = 6;
console.log(a + b); // 26
console.log(a - b); // 14
console.log(a * b); // 120
console.log(a / b); // 3.33
console.log(a % b); // 2
Assignment Operators in JavaScript
The assignment operators in JavaScript are used to assign or update values in variables.
Operator | Example | Meaning |
---|---|---|
= | x = 5 | Assigns 5 to x |
+= | x += 3 | Same as x = x + 3 |
-= | x -= 2 | Same as x = x - 2 |
*= | x *= 4 | Same as x = x * 4 |
/= | x /= 2 | Same as x = x / 2 |
%= | x %= 3 | Same as x = x % 3 |
๐ Example:
let score = 50;
score += 10; // 60
score -= 5; // 55
score *= 2; // 110
console.log(score);
Comparison Operators in JavaScript
The comparison operators in JavaScript are used to compare two values. They return true
or false
.
Operator | Example | Result |
---|---|---|
== | 5 == "5" | true (checks value only) |
=== | 5 === "5" | false (checks value + type) |
!= | 5 != "5" | false |
!== | 5 !== "5" | true |
> | 10 > 5 | true |
< | 10 < 5 | false |
>= | 10 >= 10 | true |
<= | 10 <= 9 | false |
๐ Example:
console.log(10 > 5); // true
console.log(10 === "10"); // false
console.log(10 !== 5); // true
Logical Operators in JavaScript
The logical operators in JavaScript are used to work with multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& | AND | (5 > 3 && 10 > 5) | true |
` | ` | OR | |
! | NOT | !(5 > 3) | false |
๐ Example:
let age = 20;
console.log(age > 18 && age < 30); // true
console.log(age > 25 || age < 18); // false
console.log(!(age > 18)); // false
Real-Life Example: Online Shopping Discount
Letโs apply operators in a real-world scenario:
let cartValue = 1200;
let isMember = true;
// Apply discount if cart > 1000 OR user is a member
if (cartValue > 1000 || isMember) {
console.log("You get a discount!");
} else {
console.log("No discount available");
}
Here we used comparison operators and logical operators in JavaScript to make a decision.
Key Takeaways
- Arithmetic operators help with calculations.
- Assignment operators simplify updating variables.
- Comparison operators help in decision-making.
- Logical operators combine multiple conditions.
Without operators, JavaScript would just store values but never process them.
External Resource
For more details, check the official MDN documentation on JavaScript operators.
Whatโs Next?
In this JavaScript tutorial series:
- โ Day 1 โ First JavaScript Code
- โ Day 2 โ Variables in JavaScript
- โ Day 3 โ Data Types in JavaScript
- โ Day 4 โ Operators in JavaScript
- ๐ Day 5 โ If-Else Statements in JavaScript
By Day 5, youโll start controlling the flow of logic in your programs.