JavaScript Validator
What is a JavaScript validator?
A JavaScript validator (people also say JS validator or JS checker) reads your code and tells you
what’s wrong with it before you load it in a browser. Paste a script on the left and the list on the right shows
every problem it finds, with the line and column, in plain words instead of “Unexpected token”.
It checks two things. First the syntax: a missing }, a comma that isn’t there, a string that
never ends. Those stop the whole script, so the browser runs none of it. Then, once the code reads fine, it looks
for mistakes that run but do the wrong thing, like if (size = 'large') or a variable name with a typo.
Your code is never run, and it stays in your browser.
How to use it
Step 1 Paste or open your code
Paste JavaScript into the left panel, open a .js file with the upload button, or drop the file
onto the page. Sample loads a short coffee-order script with a few mistakes hidden in it. There’s no
Check button: the list updates a moment after you stop typing.
Step 2 Read the list and jump to each problem
Every row has a badge, the spot (line and column), a plain-English message and a short rule name.
- Error (red): the code won’t run like this. Fix these first.
- Warning (amber): it runs, but probably not the way you meant.
- Tip (blue): a habit worth changing, such as
== instead of ===. Untick Show tips to hide them.
Click a row and the validator selects that spot in the editor. On a phone it scrolls back up to the code for
you. The filter above the list shows only errors, warnings or tips.
Step 3 Tell it where the code runs
- Runs in: Browser knows names like
document, window and
localStorage. Node.js knows require, module and process.
Pick both for code that runs in either.
- Read as: leave it on auto unless you know better. Script is a normal
<script> tag; ES module is <script type="module">. More on that
below.
Fix something and watch the row disappear. When you’re done, Copy report copies the list as
plain text for a ticket or a chat message, and the download button saves it as a .txt file. Your code and
options are remembered in this browser for next time.
Reading a syntax error
A syntax error means the browser couldn’t even read the script, so none of it runs, not even the lines above the
mistake. The validator shows only the first one, because after a missing bracket everything below is guesswork.
Under each message you’ll also see what the parser said word for word (“Parser says: Unexpected token”), in
case you want to search for it.
A missing } (the most common one)
function greet(name) {
if (name) {
return 'Hello, ' + name;
// the } for the if is missing
}
The validator says: “The code ends before everything is closed: a } is missing. Going by the indentation, it’s probably the { on line 2 that never gets its }.” The parser only notices at the very end of the file, but the
indentation gives it away: the } on the last line lines up with function, so it
closes the function, and the if on line 2 is left open.
A missing comma in an object
❌ Before:
const order = {
drink: 'Latte'
size: 'large'
};
✅ After:
const order = {
drink: 'Latte',
size: 'large'
};
The validator says: “A comma is missing between two properties: put a , after 'Latte' at the end of line 2.”
Curly quotes from a chat app or a document
const hello = “Hello, Sam”;
The validator says: ““ is a curly quote, the kind word processors and chat apps put in. JavaScript only understands straight quotes: " ' and `. Retype the quotes on this line.” This happens a lot with code copied out of Word, Google Docs or
a messaging app, which swap straight quotes for pretty ones as you type.
Mistakes it catches after the code reads fine
These all run without a syntax error, which is why they’re easy to miss. The validator only flags them when it’s
sure. It would rather stay quiet than send you chasing a problem that isn’t there.
= instead of === in an if
❌ Before: the alert shows for every size
let size = 'small';
if (size = 'large') {
alert('Big cup!');
}
✅ After:
let size = 'small';
if (size === 'large') {
alert('Big cup!');
}
The validator says: “This if gives size a new value instead of comparing it, because = assigns. To compare, use ===. If the assignment is on purpose, wrap it in an extra pair of parentheses.” One = sets a value, === compares.
size = 'large' sets size to 'large', and a non-empty string counts as true, so the if always passes.
A name with a typo, or one that’s never declared
❌ Before:
const total = 4.2;
alert('You owe ' + totl);
✅ After:
const total = 4.2;
alert('You owe ' + total);
The validator says: “totl is used but never declared in this code. Did you mean total?” In the browser this line would stop with “totl is not defined”. The
validator knows the names browsers (or Node.js) give you for free, like document,
fetch and Math, so it only lists the ones nobody declared.
Using a library that’s loaded by another <script> tag? The validator can’t see that file, so
$('.menu').slideDown(); gets: “$ is used but never declared in this code. If it comes from another script or a library (like $ from jQuery), that’s fine: add /* global $ */ at the top to tell the checker.” A comment at the top fixes it:
/* global $ */
$('.menu').slideDown();
A .map() that never returns
❌ Before: labels is [undefined, undefined]
const prices = [3.8, 4.2];
const labels = prices.map((p) => {
'$' + p.toFixed(2);
});
✅ After:
const prices = [3.8, 4.2];
const labels = prices.map((p) => '$' + p.toFixed(2));
The validator says: “This .map() callback never returns anything, so every item in the new array is undefined. Add a return, or drop the braces so the arrow returns the value by itself: => '$' + p.toFixed(2) If you only want to loop over the items, use forEach() instead.” An arrow function with { } needs a
return; without the braces it returns the value by itself. The same check covers
filter, find, some, every, reduce and
sort.
Giving a const a new value
❌ Before:
const cups = 0;
cups = cups + 1;
✅ After:
let cups = 0;
cups = cups + 1;
The validator says: “cups is a const, so it can’t get a new value. This line stops with a TypeError when it runs (Chrome says “Assignment to constant variable”). Use let if the value has to change.” This one is an error, not a warning, because the line always fails
when it runs. Changing what’s inside a const object (cart.items = []) is fine; only the name itself
is locked.
Comparing with NaN
❌ Before: the alert never shows
const tip = Number('ten');
if (tip === NaN) {
alert('Please type a number');
}
✅ After:
const tip = Number('ten');
if (Number.isNaN(tip)) {
alert('Please type a number');
}
The validator says: “Comparing with NaN never works: nothing equals NaN, not even NaN itself. Use Number.isNaN(tip) instead.”
A line that starts with (
❌ Before:
const greet = function () { alert('Hi, Sam'); }
(function () { alert('Page ready'); })();
✅ After:
const greet = function () { alert('Hi, Sam'); };
(function () { alert('Page ready'); })();
The validator says: “This line starts with (, so JavaScript glues it onto the end of the line above and reads it as a function call. Put a ; at the end of the line above.” JavaScript adds missing semicolons for you at most line ends, but not
before a line that starts with (, [ or a backtick. Here the second line becomes a call of
the first function: “Hi, Sam” pops up when nobody asked for it, and then the script stops with a TypeError. If you leave semicolons out, put one at the
start of such a line: ;(function () { ... })().
It also looks for code after a return that can never run, the same key twice in one object, the
same case twice in a switch, a case without break that falls
into the next one, typeof x === 'strng', an if (...); with a stray semicolon, empty
catch blocks and console.log calls you may have forgotten.
What it can’t check
Reading code without running it only gets you so far. These problems only show up while the code runs, so the
validator says nothing about them:
To find runtime problems, run the code and watch the console. The JavaScript
Playground does that on this site, with a console under the code.
Script or module?
The same code can be fine as an ES module and broken as a normal script. A module is loaded with
<script type="module"> or imported by another file. It can use import,
export and await outside a function, and it’s always in strict mode. On auto, the
validator reads code with import or export as a module and everything else as a script.
If a script only works as a module, it says so:
const res = await fetch('/menu.json');
const menu = await res.json();
The validator says: “await outside a function only works in an ES module, so the code was read as one. Load it with <script type="module">: in a normal <script> tag it stops with a syntax error.”
Next steps
Frequently asked questions
Does the validator run my code?
No. It reads the code the way a browser does before running it, builds a map of it, and checks that map. Nothing is executed, so an alert(), a loop that never ends or a fetch() to your server won’t happen. The check also runs in your browser, so the code isn’t uploaded anywhere.
Why does it only show one syntax error?
Because the parser can’t read past it. After a missing } or a stray comma, everything below is guesswork, and guessing would give you a list of errors that aren’t real. So you get the first one, with the line and column. Fix it and the check runs again straight away, either showing the next error or the warnings for the whole file.
What’s the difference between errors, warnings and tips?
An error means the code won’t run as it is: a syntax error, or giving a const a new value. A warning is code that runs but almost certainly doesn’t do what you meant, like if (size = "large") or a .map() callback with no return. A tip is about style or habits, like == instead of === or a console.log left in. You can hide tips with the Show tips box.
It says a variable is never declared, but it comes from a library. What do I do?
The validator only sees the code you paste, so $ from jQuery or gtag from Google Analytics look undeclared. Add a comment like /* global $, gtag */ at the top and the warning goes away. For Node.js code, set Runs in to Node.js so require, module and process count as known names.
Can it check TypeScript or JSX?
No, it reads plain JavaScript only. If you paste TypeScript (const total: number = 0) or JSX (<div>Hi</div>), it tells you so instead of showing a confusing error. The TypeScript Playground on this site turns TypeScript into JavaScript, and the React Playground runs JSX.
Will it find runtime errors, like “Cannot read properties of null”?
Only a few, like assigning to a const. Most runtime errors depend on data that only exists while the code runs: an element that isn’t on the page, a fetch that fails, a value that turns out to be null. To catch those, run the code and watch the console, for example in the JavaScript Playground.
What does “Read as script or module” change?
A module is a file loaded with <script type="module"> (or imported by another file). Modules can use import, export and await at the top level, and they’re always in strict mode, so a few old things like with or the number 010 become errors. Auto picks module when the code has import or export, and falls back to module when only top-level await or import.meta makes it work.
Is this the same as ESLint or JSHint?
No. A linter in your editor can check a whole project with rules your team picks. This page is a quick check with no setup: paste, read the list, fix. It covers the mistakes that cause real bugs most often. Many of the short rule names next to each message, like no-undef, eqeqeq and no-unreachable, match ESLint rules that check the same thing, so you can search for them.
Does it support modern JavaScript?
Yes. Optional chaining (user?.name), ??, classes with #private fields and static blocks, async and await, BigInt, numeric separators (1_000_000) and the other recent additions all read fine. It doesn’t tell you whether an old browser supports them, though.