Unexpected token means the JavaScript parser hit a character it didn't expect while reading your code. This error appears in the browser console, Node.js, and TypeScript compilation — it's a syntax error, not a runtime bug, so your code never executes.
What "Unexpected token" Means
The JavaScript engine reads your file character by character, expecting a specific structure based on the language grammar. When it finds something that doesn't fit — a stray }, a missing ), or an invalid character — it throws this error. The message usually includes the token and line number, like Unexpected token '}' at line 5.
Why It Happens
The two most common causes are mismatched brackets and missing commas in object literals.
First, mismatched brackets: you open a function call with ( but close it with } instead of ). The parser sees } where it expects ), and the error fires.
Second, missing commas: in an object literal {a: 1 b: 2}, the parser hits b right after 1 with no comma. It doesn't know what to do, so it throws.
Example Code That Triggers It
Here's a minimal snippet that produces this exact error in Node.js or the browser console:
const user = {
name: "Alice",
age: 30
email: "alice@example.com" // Missing comma after 30
};
console.log(user);
Run this in Node.js and you'll get:
SyntaxError: Unexpected token, expected "," (3:2)
The parser hits email after 30 with no comma, and it can't continue.
How to Fix It
The fix is simple — add the missing comma:
const user = {
name: "Alice",
age: 30,
email: "alice@example.com" // Now it parses correctly
};
console.log(user);
This works because the JavaScript grammar requires a comma between object properties. Without it, the parser can't determine where one property ends and the next begins. The same logic applies to arrays — missing commas between elements trigger the same error.
Common Mistakes That Cause This
Mistake 1: Copy-pasting code with smart quotes. If you paste code from a Word doc or a blog with typographic quotes (' or "), the parser sees a Unicode character it doesn't recognize. Use a code editor with syntax highlighting — it will visually flag these.
Mistake 2: Editing JSX in React without closing tags. In React or Next.js, forgetting to close a JSX element like <div> produces Unexpected token at the end of the file. The parser expects </div> but finds the end of the file instead. Always check that every JSX tag has a matching closing tag.
When Should You Worry About This?
You should worry when the error points to a line that looks correct. That means the real problem is earlier in the file — a missing closing brace or parenthesis that shifts the entire parse. For example, if you forgot a } at the end of a function, the parser might report Unexpected token at the very last line of your file, which looks fine. This is a classic "error cascade" — fix the original mistake and the cascade disappears.
Next time you see this, check the line number in the error message first — but if it looks clean, scroll up a few lines and look for an unclosed bracket or parenthesis. That's where the real problem lives.