How to Validate JavaScript - Step by Step Guide
Add your JavaScript
Any plain JavaScript works, whether it is a browser script, a Node file or an ES module:
Example: JavaScript Code to Check
A small function like this one is enough to try it out:
function calculateTotal(items) { let total = 0; for (let i = 0; i < items.length; i++) { total += items[i].price; } return total; }
Read the result
About half a second after you stop typing, the code is parsed. Nothing is run. The validator:
Example: Validation Results
The validator provides detailed feedback:
No syntax errors found. Read as a classic script. It parses as an ES module too.
Read the Error
When the code doesn't parse, you see the first error and where it is:
Fix and check again
Fix the code right in the editor and the result follows along:
Frequently Asked Questions
What exactly does this validator check?
Whether your code parses as JavaScript, the same question Node answers with node --check. That catches unclosed brackets, strings, template literals, comments and regular expressions, a missing comma, a const without a value, the same let declared twice, await outside an async function, break outside a loop, a broken regex pattern and so on. You get the line, the column and the line itself with a caret under the spot.
Why does it say my code was read as a script, or as an ES module?
JavaScript can be loaded two ways, and they follow slightly different rules. A file with import or export, top-level await or import.meta only works as an ES module (type="module" in a browser, a .mjs file or "type": "module" in Node). Code with a with statement or an old-style octal like 010 only works as a classic script, because modules are always in strict mode. The validator tries both and tells you which one your code passes as.
Does it find bugs, type errors or style problems?
No. Valid here means the code can be parsed; it says nothing about whether it does the right thing. The code is never run, so a misspelled variable, a call to a function that doesn't exist, or a value of the wrong type will not show up. For a few common mistakes such as == instead of === or unreachable code, try the JavaScript Linter. For layout, use a formatter.
What happens if I paste JSX, TypeScript or JSON?
Each is named instead of getting a confusing parser message. JSX and TypeScript are not plain JavaScript, so browsers and Node reject them without a build step; the error points you to the JSX Validator or the TSX Validator. A JSON object is reported as JSON data, since a { at the start of a file reads as a block of code. A JSON array happens to be valid JavaScript, so it passes, with a note that it is data rather than a program.
Why does a top-level return pass with a warning?
Node wraps every CommonJS file in a function, so a return at the top of the file is legal there and is a common way to stop a script early. Browsers and ES modules reject it. The validator lets it through, the way node --check does for a .cjs file, and adds a warning so you know it will fail anywhere else.
Is my code uploaded or kept anywhere?
No. The check runs in your browser; the code is not sent to our server and nothing is kept. Reload the page and the editor starts empty. Copy and Download give you the validation report as plain text, not a changed copy of your code.
Related tools: JavaScript Linter, JSX Validator, TSX Validator and JavaScript Beautifier.