JavaScript - Comma & Grouping Operators
Overview
Estimated time: 10–15 minutes
The comma operator evaluates multiple expressions and returns the last. Grouping with parentheses controls precedence.
Learning Objectives
- Use grouping to make precedence explicit.
- Recognize the comma operator and avoid overuse.
Prerequisites
Examples
// Grouping
const r = (2 + 3) * 4; // 20
// Comma: evaluate left to right, returns last
let i = 0;
const val = (i += 1, i += 2, i); // i=3, val=3
Common Pitfalls
- Comma operator can hurt readability; prefer separate statements unless truly concise and clear.