How to Lint JavaScript - Step by Step Guide
Add your JavaScript
Any plain JavaScript works, from a three-line snippet to a whole file:
Example: JavaScript Code to Lint
This function runs, but it has a few problems a reviewer would point out:
function calculateSum(a, b) {
var result = a + b;
if (result == 0) {}
return result;
console.log(result);
}Read the results
About half a second after you stop typing, the code is parsed. If it doesn't parse, you see the first syntax error with a pointer to it and nothing else. If it does, these eight ESLint rules run on the syntax tree, so text inside strings, comments and regular expressions is never mistaken for code:
eqeqeq: flags == and != (loose comparison converts types first; use === and !==)no-var: flags var declarations (let and const keep a variable inside its block)no-debugger: flags debugger statements left in the codeno-console: notes console.log and other console calls, unless you declared your own consoleno-unreachable: flags code after return, throw, break, continue, or a loop that never endsno-cond-assign: flags = used as an if, while or ternary condition, unless wrapped in an extra pair of bracketsno-empty: flags empty blocks and switch statements (empty functions and blocks with a comment are fine)no-dupe-keys: flags an object literal that repeats a key, so one value silently overwrites the otherThat's the whole list. It is not a full ESLint run: there's no config, no plugins, and no unused-variable or style checks.
Example: Linting Results
For the function above, the page reports:
- Line 2, column 3 · Warning
no-var - Line 3, column 14 · Warning
eqeqeq - Line 3, column 20 · Warning
no-empty - Line 5, column 3 · Note
no-console - Line 5, column 3 · Warning
no-unreachable
Fix and check again
Change the code right in the editor; while it is being re-checked the results panel says Checking…, so you never read a result for older text. Findings come in three kinds:
Want the code itself tidied up? The JavaScript Formatter handles layout, and the JavaScript Validator is there when all you need is a yes or no on the syntax.
Frequently Asked Questions
Which rules does this linter check?
First it parses your code; a syntax error is reported on its own with the line, column and a pointer to the spot. If the code parses, it runs eight rules that ESLint also has, with their default settings: eqeqeq, no-var, no-debugger, no-console, no-unreachable, no-cond-assign, no-empty and no-dupe-keys. Every finding shows the rule name, so you can look it up in the ESLint docs.
Is this the same as running ESLint?
No. It covers only those eight rules, and for them it reports the same lines and columns ESLint does. There is no config file, no plugins, no autofix, and no checks for unused or undefined variables, React hooks or code style. For a real project, run ESLint itself; this page is for a quick look at a snippet or a single file.
Why don't I get warnings about semicolons, spacing or indentation?
Those are style questions that teams settle differently, and a formatter handles them better than warnings do. This page sticks to things that are usually real mistakes. If you want the layout tidied, run the code through a JavaScript formatter instead.
What happens with JSX, TypeScript or JSON?
JSX is accepted, so React components in .js or .jsx files are checked like any other code. TypeScript is recognised and named, but not linted: type annotations and interfaces make a .js file fail, so you get that message instead of rule warnings. JSON pasted by mistake is named as JSON.
Can it fix the problems for me?
No, it only reports them. Your code in the editor is never changed. Copy and Download give you the report as plain text, with each finding on its own line, which is handy for a code review or a ticket.
Is my code uploaded anywhere?
No. The parsing and the checks run in your browser, and the code is not sent to our server or kept anywhere. Reload the page and the editor starts empty.