Operators: Combining Values
Operators are the small symbols that combine values into new ones. + adds. == compares. && combines booleans. There aren't many, and they all show up daily.
From Bee: = assigns. == compares (loosely). === compares strictly. Read this twice. Then read it once more.
What Is an Operator?
An operator is a symbol that performs an operation on one or more values (called operands). 5 + 3 uses the + operator on operands 5 and 3.
Operators fall into a few categories:
- Arithmetic β
+ - * / % - Comparison β
== != < > <= >= - Logical β
&& || !(Python usesand or not) - Assignment β
= += -= *= /=
Precedence (Order of Operations)
Like math: * and / happen before + and -. Parentheses force a different order. When in doubt, add parentheses β readers will thank you.
== vs ===
JavaScript has both. == compares with type coercion ("5" == 5 is true). === compares strictly ("5" === 5 is false). Always prefer === to avoid surprise. Python and Java only have one equality operator.
Code Examples in Three Languages
// Arithmetic
5 + 3 // 8
10 % 3 // 1 (modulo: remainder)
// Comparison
5 === 5 // true
"5" == 5 // true (loose) β avoid
"5" === 5 // false (strict) β prefer
// Logical
true && false // false
true || false // true
!true // false
// Compound assignment
let x = 10;
x += 5; // x is now 15# Arithmetic
5 + 3 # 8
10 % 3 # 1
10 // 3 # 3 (integer division)
# Comparison
5 == 5 # True
5 != 4 # True
# Logical (words, not symbols)
True and False # False
True or False # True
not True # False
# Compound
x = 10
x += 5 # x is now 15// Arithmetic
5 + 3; // 8
10 % 3; // 1
10 / 3; // 3 (integer division for ints)
// Comparison β use .equals() for strings!
5 == 5; // true
"hi".equals("hi"); // true (== compares references for objects)
// Logical
true && false;
true || false;
!true;
// Compound
int x = 10;
x += 5;Best Practices
- Prefer
===over==in JavaScript. - Use parentheses to make precedence obvious.
- Use
%(modulo) for "every Nth" patterns. - In Java, use
.equals()to compare strings, not==.
Common Mistakes
- Confusing
=with==. Single = is assignment. - Integer division surprise.
5 / 2is2in Java/Python (with//);2.5in JS. - Operator precedence trap.
true || false && falseevaluates && first. - Comparing strings with
==in Java. Returns whether two object references match β usually false even for equal text.
We have a dedicated Common Bugs with Operators page β five real broken snippets with the fix. Read it before you start writing.
How It Works Under the Hood
Operators are syntactic sugar for function calls. 5 + 3 in JS is roughly equivalent to calling an internal + operation on the numbers 5 and 3. In Python, 5 + 3 calls (5).__add__(3) β which is why operator overloading works (you define __add__ on your class and + "just works").
Precedence is built into the language grammar. 2 + 3 * 4 parses as 2 + (3 * 4) because * has higher precedence than +. When in doubt, parentheses force the order β and make code readable to humans, who don't carry a precedence table in their heads.
Short-circuit evaluation is more than a perf trick: it's a control-flow tool. user && user.name means "if user exists, get its name" without crashing if user is null. Modern JS adds ?. for the same idea.
From Beginner to Pro
The same concept gets deeper as you grow. Here's what mastery looks like at three levels:
You add, subtract, compare with === / ==, combine with && / ||.
You distinguish = (assign), == (loose), === (strict). You use compound assignment (+=) and the ternary operator. You've been bitten by precedence at least once.
You use short-circuit for control flow (val ?? default, obj?.field). You know operator overloading rules (Python), avoid bitwise where logical was meant, and use % normalization tricks for circular indexing.
Performance & Gotchas
- JavaScript loose equality compares with type coercion. Always use
===. - Integer division works differently across languages (
5/2is 2 in Java, 2.5 in JS, 2.5 in Python 3). - Modulo with negatives isn't consistent β JS returns
-1 % 3 = -1, Python returns2. - Bitwise
&vs logical&&β single character is bitwise, almost never what you want in a condition.
Authoritative references: MDN β operators, Python β operator precedence, Oracle β Java operators.
Quick Quiz
Real-World Uses (Production Code, Today)
Pricing rules
"If subtotal > $50 free shipping" β that's comparison + conditional. Stack three of those and you have a real e-commerce ruleset.
Permissions
(user.isAdmin || user.isOwner) && user.isActive β three logical operators, one access decision.
Pagination
Page N starts at (N - 1) * pageSize. Every paginated UI on the internet uses this arithmetic.
Frequently Asked Questions
What does the % operator do?
Modulo β returns the remainder of integer division. 10 % 3 is 1.
Why does Python use "and" / "or" instead of && / ||?
Python prefers English keywords for readability. They behave the same as && and ||.
What's the difference between a + b and a += b?
a + b evaluates a value (doesn't change a). a += b reassigns a to a + b.
Is ! the same as not?
Yes. JavaScript and Java use !; Python uses not.
What's the ternary operator?
A one-liner conditional: condition ? a : b. Returns a if true, b if false. Python's version is a if condition else b.
Are there bitwise operators?
Yes β &, |, ^, <<, >>. Useful for low-level work; rarely seen in beginner code.
What is short-circuit evaluation?
Logical operators stop early when the result is determined. false && anything is false without checking anything.
Why does "5" + 3 produce different results in different languages?
JavaScript coerces 3 to a string. Python and Java refuse. Always be explicit about types.
Related Concepts
Data Types in Programming
Related concept.
Strings
Related concept.
Conditionals
Related concept.
- Operators: Combining Values is one of the 13 universal concepts of programming.
- The syntax differs across languages, but the underlying idea is the same.
- Practice in the playground to make it stick.