Sub topic 1
Linters and Formatters Solve Different Problems
JavaScript projects usually run two kinds of tools on their source code: a linter and a formatter. They both read your code and complain about it, so people often mix them up. They catch different problems, though, and knowing which tool owns which problem makes the rest of this tutorial easier to follow.
Prerequisites
This chapter assumes you can:
- Read modern JavaScript syntax, including ES modules and arrow functions
- Run Node.js and npm commands from a terminal
- Edit
package.jsonand other JSON files - Use basic Git commands such as staging and committing
What a linter does
A linter analyzes code for likely bugs and risky patterns. ESLint parses your file into an abstract syntax tree (AST), which is a structured representation of the code, and runs rules against it. A rule might flag a variable that is never used, a comparison with == that can coerce types in surprising ways, or a reference to an undefined name.
Lint rules deal with meaning. They ask whether the code is likely to do what you intended.
What a formatter does
A formatter rewrites code into a consistent layout. Prettier throws away your original whitespace, line breaks, and quote style, then prints the code again using its own rules. It does not care whether the code is correct. It only cares how the code looks.
Formatting deals with appearance. Two files that differ only in formatting behave identically at runtime.
One file, two kinds of problems
const total = (items) => { let sum = 0
for (const item of items) { if (item.price == "0") continue; sum += item.price }
const unused = 42
return sum }
console.log(total([{ price: 5 }, { price: "0" }, { price: 3 }]));This runs and prints 8. Prettier would fix the cramped braces, missing semicolons, and inconsistent line breaks. It would leave == and the unused variable alone, because those affect behavior. ESLint would flag unused and, with eqeqeq enabled, the loose comparison.
| Concern | Linter (ESLint) | Formatter (Prettier) |
|---|---|---|
| Unused variables | Yes | No |
| `==` vs `===` | Yes | No |
| Indentation and line length | Possible, but discouraged | Yes |
| Quote style and semicolons | Possible, but discouraged | Yes |
| Changes runtime behavior when fixing | Sometimes | Never |
Where the overlap causes trouble
Older ESLint setups included style rules for indentation, spacing, and quotes. Run those alongside Prettier and the two tools can disagree. One rewrites a line and the other reports it. The usual fix is to let ESLint handle code quality and give Prettier full control of formatting. A later chapter covers how to set that up.
Exercise
A teammate wants Prettier to catch a function that is declared but never called, and wants ESLint to enforce single quotes. Which tool should own each concern, and why?
Show answer
ESLint should catch the unused function, because spotting dead code means analyzing what the code means, and Prettier does no such analysis. Prettier should enforce single quotes, because quote style is purely about appearance. Leaving quote style to ESLint risks conflicts with Prettier's output.