Operators & Expressions
Master JavaScript operators! Learn arithmetic, comparison, logical operators, and how to build complex expressions.
Master JavaScript operators! Learn arithmetic, comparison, logical operators, and how to build complex expressions. This hands-on tutorial focuses on practical implementation of operators & expressions concepts.
Operators & Expressions
Operators are symbols that perform operations on values. They're the building blocks for creating logic and calculations in your code!
1. What are Operators? ⚙️
What is it?
Operators are special symbols (like +, -, =, !) that perform operations on one or more operands (values/variables).
Why use it? They allow you to manipulate data—add numbers, compare values, combine logic, and assign results.
How it works:
Operand1 Operator Operand2 (e.g., 5 + 3).
2. Arithmetic Operators ➕➖
Used for performing mathematical calculations.
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Modulus/Remainder)**(Exponentiation)
3. Comparison Operators 🔍
Used to compare two values. They always return a Boolean (true or false).
Strict vs Loose Equality ⚠️
==(Loose Equality): Converts types before comparing. Avoid this.===(Strict Equality): Checks value AND type. Use this.
4. Logical Operators 🧠
Used to combine multiple conditions.
&&(AND): True if both are true.||(OR): True if at least one is true.!(NOT): Inverts the value.
Short-Circuit Evaluation ⚡
JavaScript stops evaluating as soon as the result is known.
false && ...-> Stops (Result is false)true || ...-> Stops (Result is true)
5. Assignment Operators 📝
Used to assign values to variables.
=(Assign)+=(Add and assign)-=(Subtract and assign)++(Increment)--(Decrement)
6. Ternary Operator ❓
A shorthand for if-else statements.
Syntax: condition ? trueValue : falseValue
7. Operator Precedence 📊
The order in which operators are evaluated.
()(Parentheses) - Highest**(Exponentiation)*,/,%(Multiplication/Division)+,-(Addition/Subtraction)=,+=(Assignment) - Lowest
8. Nullish Coalescing (??) 🎯
(Covered in ES6+ Features, but relevant here)
Returns the right side ONLY if the left side is null or undefined.
Practical Example: Shopping Cart Logic 🛒
AI Mentor
Confused about "JavaScript operators, precedence, short-circuit evaluation, and ternary operator"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4What is the result of 10 % 3?
Key Takeaways
✅ Arithmetic: +, -, *, /, % (Remainder), ** (Power).
✅ Comparison: Always use === (Strict) over == (Loose).
✅ Logical: && (AND), || (OR), ! (NOT).
✅ Short-Circuit: || can provide default values.
✅ Ternary: condition ? true : false is a one-line if-else.
What's Next?
Now that you can manipulate values, let's learn about Strings & Template Literals to handle text like a pro!
Keep coding! 🚀