Ad Space — Top Banner

Invalid or unexpected token

JavaScript Programming Language

Severity: Critical

What Does This Error Mean?

This error means JavaScript found a character in your code that it does not recognize or did not expect in that position. It could be a special character that looks like a regular character but is not, a character from a different encoding, or a simple typo. JavaScript cannot parse the file at all until this is fixed.

Affected Models

  • All browsers
  • Node.js
  • Deno
  • Any JavaScript runtime

Common Causes

  • Pasting code from a website or PDF that introduced invisible or 'smart' characters
  • A string that was not properly closed before the end of the line
  • A non-breaking space or special Unicode character that looks like a regular space
  • Curly quotes (' ') used instead of straight quotes (' ') — common when typing in word processors
  • A special symbol from another language or character set in a place JavaScript does not expect it

How to Fix It

  1. Go to the exact line number shown in the error. Look very carefully at each character — especially quotation marks and spaces.

    Try deleting and retyping any quotation marks on that line. Curly quotes (“” ‘’) look like regular quotes but JavaScript cannot read them.

  2. If you pasted code from a website, blog post, or PDF, delete it and retype it manually. These sources often convert straight quotes to curly quotes automatically.

    This is one of the most common causes — a blog post looks correct but the quotes are decorative, not code-valid.

  3. Check for non-breaking spaces. These look identical to regular spaces but have a different character code. Delete any spaces on the problem line and retype them.

    In VS Code, you can enable 'Render Whitespace' in settings to see non-standard spaces as different shapes.

  4. Make sure all strings are on a single line or use template literals for multi-line strings. A regular string cannot span multiple lines.

    If you need a multi-line string, use backticks: let text = `This can span multiple lines`;

  5. Try copying your code into a plain text editor (like Notepad on Windows) and back again. This strips out invisible formatting characters.

    Alternatively, use VS Code's 'Change File Encoding' feature to inspect the file for unusual characters.

When to Call a Professional

This error is always fixable by the developer. It is most often caused by copy-pasting code from formatted sources like websites or documents. No professional help is needed — just careful inspection of the characters in the problem area.

Frequently Asked Questions

What are 'smart quotes' and why do they break JavaScript?

Smart quotes are the curved quotation marks (“hello” or ‘hello’) used in word processors and many websites for aesthetic reasons. JavaScript expects straight, simple quotes ("hello" or 'hello') to mark the beginning and end of a string. When JavaScript sees a curly quote, it does not recognize it as a string delimiter and throws an error. Always write code in a proper code editor, never in Microsoft Word or Google Docs.

How can I find invisible characters in my code?

In VS Code, open the Command Palette (Ctrl+Shift+P), type 'Render Whitespace', and enable it. You can also install the 'Highlight Bad Chars' extension which highlights non-standard characters. Another method: paste your code into a plain text hex editor to see the raw character codes.

Can this error come from a file encoding issue?

Yes. If a file is saved in a different encoding (like Windows-1252 instead of UTF-8), special characters in comments or strings might appear as garbled symbols. In VS Code, you can see the current file encoding in the bottom status bar. If it shows anything other than UTF-8, click it and re-save the file as UTF-8.