Operators
let a = 10, b = 3;
console.log(a + b, a - b, a * b); // 13 7 30
console.log(a / b); // 3.333...
console.log(a % b); // 1 (remainder)
console.log(a ** 2); // 100 (power)
a += 5; // a = a + 5Comparison and logical operators
console.log(5 === 5, 5 !== 6); // true true
console.log(5 > 3, 5 <= 4); // true false
console.log(true && false); // false (AND)
console.log(true || false); // true (OR)
console.log(!true); // false (NOT)if, else if, else
<script>
const marks = 72;
let grade;
if (marks >= 90) {
grade = "A+";
} else if (marks >= 75) {
grade = "A";
} else if (marks >= 60) {
grade = "B";
} else {
grade = "Needs improvement";
}
document.write("Marks: " + marks + ", Grade: " + grade);
</script>switch
const day = "Sat";
switch (day) {
case "Sat":
case "Sun":
console.log("Weekend");
break;
default:
console.log("Weekday");
}Ternary operator
const status = marks >= 40 ? "Pass" : "Fail";Truthy and falsy
false, 0, "", null, undefined and NaN are falsy. Everything else is truthy.
const name = "";
console.log(name || "Guest"); // "Guest"Practice
Write a program that takes a number and prints whether it is positive, negative or zero, and whether it is even or odd.
Practice tasks
Reading is not enough. Try these without copying code, then compare with the lesson.
- Use === and == on several pairs and note the difference.
- Write an if/else chain that classifies marks into grades.
- Use the ternary operator for a short condition.
Common mistakes
- Using = instead of === in a condition.
- Relying on truthy and falsy values without checking.
Quick check
Why prefer ===?
It compares without converting types.
What does && do?
Returns true only if both sides are true.