Operators
intermediatePart of Core JavaScript
Theory
Operators are symbols that perform operations on values (operands). JavaScript provides a rich set of operators for various tasks.
Arithmetic operators handle mathematical calculations:
+(addition),-(subtraction),*(multiplication),/(division),%(modulus/remainder),**(exponentiation)
The + operator also performs string concatenation when one operand is a string. The % operator returns the remainder of division, useful for checking even/odd numbers or cycling through ranges.
Assignment operators assign values to variables. The basic = assigns a value. Compound assignment operators combine arithmetic with assignment:
+=,-=,*=,/=,%=,**=
let x = 10;
x += 5; // x = x + 5 → 15
x *= 2; // x = x * 2 → 30Comparison operators compare values and return a boolean:
==(loose equality — coerces types)===(strict equality — no coercion)!=(loose inequality),!==(strict inequality)>,<,>=,<=
Always prefer === over == to avoid unexpected type coercion results. For example, 0 == false is true but 0 === false is false.
Logical operators work with boolean values:
&&(AND) — returnstrueif both operands are truthy||(OR) — returnstrueif either operand is truthy!(NOT) — inverts the boolean value
Logical operators use short-circuit evaluation. With &&, if the first operand is falsy, the second is never evaluated. With ||, if the first operand is truthy, the second is skipped.
The ternary operator (condition ? expr1 : expr2) is a concise alternative to if/else for simple conditions.
The nullish coalescing operator (??) returns the right operand only when the left operand is null or undefined. Unlike ||, it does not treat 0, '', or false as falsy.
let username = null;
let displayName = username ?? "Guest"; // "Guest"Optional chaining (?.) safely accesses nested properties without throwing an error if an intermediate value is null or undefined.
Operator precedence determines the order in which operators are evaluated. Multiplication and division have higher precedence than addition and subtraction. Use parentheses () to override precedence and make expressions clearer.
Practical Examples
Exercises
Temperature Converter
Write code that converts a temperature from Celsius to Fahrenheit using the formula: F = C * 9/5 + 32. Use the ternary operator to display whether the temperature is 'Hot' (above 86°F) or 'Cool'.
Starter Code:
const celsius = 30;\n// Calculate fahrenheit and print statusExpected Output:
86°F - HotLogin Validator
Create variables for username and password. Use logical operators to validate: username must be at least 4 characters AND password must be at least 8 characters. Print 'Access Granted' or 'Access Denied'.
Starter Code:
const username = 'john_doe';\nconst password = 'secret123';\n// Your validation logic hereExpected Output:
Access GrantedShopping Cart Calculator
Calculate the total price of items in a cart. Use compound assignment operators. Apply a 10% discount if the total exceeds $100 using the ternary operator. Use ?? to default to 0 if values are null.
Starter Code:
let total = 0;\nconst item1 = 25.99;\nconst item2 = 89.99;\nconst coupon = null;\n// Calculate total with discount if applicableExpected Output:
Total: $115.98\nDiscounted: $104.38Mini Quiz
Mini Quiz
Mini Project
Mini Project: Grade Calculator
Create a program that calculates a student's final grade based on three exam scores. Use arithmetic, comparison, and logical operators to compute the average and assign a letter grade.
Requirements:
Bonus Challenge
Add a pass/fail check using && — student must score above 60 in ALL subjects to pass.