How to use the JavaScript validator
- Paste your code into the JavaScript box.
- Read the result. Valid code shows No syntax errors found. with the number of lines, top-level statements and functions, and whether it is a script or an ES module.
- Invalid code shows the line and column of the problem and a Where the parser stopped frame with a caret under the spot.
- If the parser recovered from more than one problem, an All errors table lists each one.
What it does and when to use it
Use it when a build or a browser console says “Unexpected token” and you want to find the spot in a pasted snippet. It also helps to check a code sample before you put it in documentation, or to test code copied from an email or chat where quotes and brackets often get damaged.
The tool checks syntax only. Once the code is valid, the JavaScript formatter can tidy it and the JavaScript minifier can shrink it. If the error is about a name, the JS variable name validator explains which identifiers are allowed. To try the code and see its output, use the JS console simulator.
How it works
The page loads the Babel parser that ships with Prettier and builds a syntax tree from your text. Babel can recover from some mistakes and keep going, so the tool reports the first problem as the main error and lists any others in a table. Columns are counted from 1. When the code parses, the tool walks the tree to count functions (declarations, expressions, arrows and methods) and to look for import or export declarations.
Worked examples
Valid module.
function greet(name) {
return `Hi, ${name}`;
}
export default greet;
Result: Status Valid, 4 lines, 2 top-level statements, 1 function, Type ES module.
A missing closing brace. const user = { name: 'Asha', roles: ['admin'] ; gives “Syntax error — line 1, column 47: Unexpected token, expected “,”“. The caret sits on the semicolon, where the parser expected a comma or }.
Several errors at once.
let x = 1;
let x = 2;
const y;
The main error is line 2, column 5: “Identifier ‘x’ has already been declared.” The All errors table adds line 3, column 8: “Missing initializer in const declaration.”
Limits and tips
- Fix from the top. One missing bracket can cause errors further down. Fix the first error and check again.
- An error at the last line often means an unclosed
{,(or string earlier in the file. - No TypeScript. Strip types or check
.tsfiles with the TypeScript compiler. - Not a linter. Style issues and unused variables are not reported.
Frequently asked questions
Does it find runtime errors such as undefined variables?
Can I validate TypeScript?
Why is my code reported as valid when Node rejects it?
How does it decide between Script and ES module?
Is my code run or uploaded?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.