Operators and Comparisons
Operators and Comparisons
Operators compute values or compare them. Strict equality compares without converting the operands' types, and logical operators can combine school rules. Parentheses make grouping explicit when a rule contains several operations.
Before this topic, review Convert Values Explicitly if its terms are unfamiliar.
What you’ll learn
- Explain operators and comparisons through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: Both strict-equality operands are now the same numeric value.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
console.log(false);
console.log(72 >= 50 && 72 <= 100);
Expected output
false
trueRun in JavaScript compiler →Introduction to JavaScript Operators
JavaScript operators allow you to perform operations on variables and values. From basic math to complex logic evaluation, operators play a central role in making decisions, transforming data, and controlling program flow.
1. Arithmetic Operators
Used to perform basic math operations.
let a = 10;
let b = 3;
console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);
console.log("Exponentiation:", a ** b);
console.log("Increment a:", ++a);
console.log("Decrement b:", --b);
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Exponentiation: 1000
Increment a: 11
Decrement b: 2
2. Assignment Operators
Used to assign values to variables. Some also perform operations.
let x = 5;
x += 3; // Same as x = x + 3
x -= 2;
x *= 2;
x /= 3;
x %= 2;
console.log("Final value of x: " + x);
Final value of x: 2
3. Comparison Operators
Compare two values and return a boolean (true/false).
console.log(10 == '10'); // Loose equality (true)
console.log(10 === '10'); // Strict equality (false)
console.log(5 != 7); // true
console.log(5 !== '5'); // true
console.log(5 > 3); // true
console.log(5 >= 5); // true
console.log(2 < 4); // true
console.log(2 <= 1); // false
true
false
true
true
true
true
true
false
4. Logical Operators
Used for combining multiple conditions or expressions.
let a = true;
let b = false;
console.log(a && b); // AND
console.log(a || b); // OR
console.log(!a); // NOT
false
true
false
5. Bitwise Operators
Operate on binary representations of numbers.
let x = 5; // 0101
let y = 3; // 0011
console.log(x & y); // AND -> 0001 => 1
console.log(x | y); // OR -> 0111 => 7
console.log(x ^ y); // XOR -> 0110 => 6
console.log(~x); // NOT -> 1010 => -6 (2's complement)
console.log(x << 1); // Left shift => 1010 => 10
console.log(x >> 1); // Right shift => 0010 => 2
1
7
6
-6
10
2
6. Type Operators
Used to check the type of a variable or to determine if an object is an instance of a class.
let name = "Alice";
let age = 30;
console.log(typeof name); // string
console.log(typeof age); // number
class Person {}
let p = new Person();
console.log(p instanceof Person); // true
string
number
true
7. Ternary Operator (?:)
A shorthand for if...else conditions.
let score = 85;
let result = (score >= 60) ? "Pass" : "Fail";
console.log(result);
Pass
8. Nullish Coalescing Operator (??)
Returns the right-hand operand when the left is null or undefined.
let userName = null;
let displayName = userName ?? "Guest";
console.log(displayName);
Guest
9. Optional Chaining Operator (?.)
Safely access deeply nested properties without errors if intermediate is null or undefined.
let user = {
profile: {
name: "John"
}
};
console.log(user.profile?.name); // John
console.log(user.settings?.theme); // undefined (no error)
John
undefined
Conclusion
Understanding JavaScript operators helps you control your code with precision and logic. Whether you're evaluating expressions, comparing values, or chaining safe access, these operators empower you to write efficient and readable code. Practice is key — try modifying the examples and observe how changes affect the output.
Recognize the important boundary
Logical operators can return an operand rather than a boolean. Division by zero yields Infinity for ordinary numbers, so validate a divisor.
Try a small variation
JavaScript · original source for practice
console.log(false);
console.log(72 >= 50 && 72 <= 100);
Your task: Replace 72 === '72' with 72 === 72. Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
Both strict-equality operands are now the same numeric value.
JavaScript · complete worked variation
console.log(true);
console.log(72 >= 50 && 72 <= 100);
Expected output
true
trueRun in JavaScript compiler →Predict the original program
JavaScript · original program for this question
console.log(false);
console.log(72 >= 50 && 72 <= 100);
Keep the useful idea
Operators compute values or compare them. Strict equality compares without converting the operands' types, and logical operators can combine school rules. Parentheses make grouping explicit when a rule contains several operations.
Check your variation against this explanation: Both strict-equality operands are now the same numeric value.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.