Operators
JavaScript relies on specialized operators to manipulate values, compare data, or manage truthiness.
1. Arithmetic Operators
let x = 10;
x + 5; // 15
x - 2; // 8
x * 2; // 20
x / 2; // 5
x % 3; // 1 (Modulo: Remainder of division)
x ** 2; // 100 (Exponentiation)
2. Comparison Operators (== vs ===)
==(Loose Equality): Checks value only. It actively coerces types to try and find a match.===(Strict Equality): Checks value AND data type.
Always use === to avoid frustrating bugs!
5 == "5" // true (The string "5" is coerced to a number)
5 === "5" // false (Number does not strictly equal String)
5 != "5" // false
5 !== "5" // true
3. Logical Operators
Used to combine multiple conditions.
&&(AND): Evaluates to true only if BOTH sides are true.||(OR): Evaluates to true if AT LEAST ONE side is true.!(NOT): Flips the boolean truthiness of a value.
const hasDegree = true;
const hasExperience = false;
if (hasDegree && hasExperience) {
console.log("Hired!");
} else if (hasDegree || hasExperience) {
console.log("Pending Review"); // We hit this block!
}
if (!hasExperience) {
console.log("Entry level candidate");
}