Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Web

Uncaught SyntaxError: Unexpected end of input, and how to fix it

· 8 min read

You add a click handler to a button. The page loads, the button does nothing, and the console shows:

Console
Uncaught SyntaxError: Unexpected end of input

Uncaught SyntaxError: Unexpected end of input means JavaScript reached the end of your script while it was still waiting for something to close: almost always a }, sometimes a ), ] or a closing backtick. Because the error is about the end, the line number points to the end of the script, not to the place where you forgot the character. That makes it confusing, but there's a quick way to find the gap. The same words also appear in a second, unrelated situation: parsing an empty JSON response. This article covers both.

Try it yourself

Save this as index.html, open it in your browser and look at the console (F12, then Console):

index.html
<!doctype html>
<html>
<body>
  <button id="btn">Go</button>
  <script>
    const button = document.getElementById("btn");
    button.addEventListener("click", () => {
      console.log("clicked");
  </script>
</body>
</html>

In Chromium, this gave the error above. The function's { is never closed, and neither is the ( of addEventListener. The browser reads to the end of the script and stops.

What the error means

Read it in parts:

  • Uncaught: nothing in your code caught it.
  • SyntaxError: the code isn't valid JavaScript, so the browser couldn't even start running this script. Not a single line of it runs, which is why the button does nothing.
  • Unexpected end of input: the "input" is your script. It ended while JavaScript still expected more: the rest of an unfinished block, call or string.

So the question isn't "what's wrong on the last line?" It's: which opening bracket never got its partner?

Cause 1: a missing closing brace }

Every { needs a matching }. In longer code, one missing brace in the middle shifts everything after it:

app.js
function total(prices) {
  let sum = 0;
  for (const p of prices) {
    sum += p;
  }
  return sum;

The for block is closed, but the function isn't. In our test, this gave Unexpected end of input. The fix is the } after return sum;.

Deeper nesting makes it harder to see:

js
function load() {
  if (true) {
    for (let i = 0; i < 2; i++) {
      console.log(i);
    }

}

Three blocks open, two close. Which one is missing? The indentation is the clue: the empty line at the if level is where the if should close.

Cause 2: a missing ) after a callback

Event handlers and array methods take a function inside parentheses, so they end with });, a brace and a parenthesis. Forgetting the ) is very common:

js
button.addEventListener("click", () => {
  console.log("clicked");
}

Here the browser can tell what's missing, so the message is more specific:

Console
Uncaught SyntaxError: missing ) after argument list

Same family, same fix: close it with });. When the brace is missing too, as in the first example, you get Unexpected end of input instead.

Cause 3: an unclosed template string

Template strings use backticks and can span several lines. So if you forget the closing backtick, everything after it becomes part of the string, until the end of the script:

js
const message = `Hello, ${"Sam"}
Console
Uncaught SyntaxError: Unexpected end of input

Normal strings with " or ' can't span lines, so a missing closing quote gives a different message right on that line:

Console
Uncaught SyntaxError: Invalid or unexpected token

An unclosed /* comment behaves the same way: in our test, it also gave Invalid or unexpected token.

Cause 4: a script cut off by the HTML around it

If a <script> block is cut short, for example because you pasted only half of an example, or an editor merge removed a few lines, the script ends where the </script> tag is. Check that the last lines before </script> really are the end of your code.

How to find the missing character fast

Your editor can do most of the work:

  1. Format the file. In VS Code, right-click and choose "Format Document" (Shift+Alt+F on Windows, Shift+Option+F on Mac). If the indentation suddenly looks wrong from some point on, the gap is just above that point.
  2. Click a bracket. Most editors highlight the matching bracket. Click each { at the start of a function or block and see whether its partner is where you expect.
  3. Fold blocks. Click the small arrow next to a function line to fold it. If the fold swallows code that shouldn't be inside the function, that function isn't closed.
  4. Comment out halves. In a long script, comment out the second half. If the error changes, the problem is in that half.
  5. Write closing brackets right away. When you type {, type } immediately and then fill in the middle. Many editors do this for you.

The JSON version: Unexpected end of JSON input

The same words appear when JSON.parse gets an empty string:

js
JSON.parse("");
Console
Uncaught SyntaxError: Unexpected end of JSON input

And with fetch, when the server's response has no body:

Console
Uncaught (in promise) SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input

This has nothing to do with your brackets. It means the data was empty: an empty file, a server that returned nothing, or a localStorage value that was never saved. Log the response with await response.text() before calling .json(), and check response.ok. For the related "not valid JSON" errors, see Unexpected token '<', "<!DOCTYPE "... is not valid JSON.

The same error in Node.js and other browsers

Node.js gives the same message and shows the file name with the line number of the end of the file:

Terminal
app.js:3



SyntaxError: Unexpected end of input

The 3 is the last line of the file, not the line with the mistake. Firefox words it differently, for example missing } after function body, which is often more helpful, because it says what's missing.

Errors that look alike

  • Unexpected token '}': the opposite problem, an extra closing brace. In our test, one } too many gave this message.
  • Unexpected token ';': an unclosed [ or { followed by a semicolon, like const colors = ["red", "green";. The browser reached the ; while still inside the array.
  • missing ) after argument list: a missing ) in a function call (cause 2).
  • Invalid or unexpected token: an unclosed normal string or comment (cause 3).
  • Cannot read properties of null: the script ran, but an element wasn't found. That's a different kind of error. See Cannot read properties of null (reading 'addEventListener').

Wrong fixes that don't help

  • Adding a } at the very end of the file. The error may go away, but code that belonged after a function is now inside it. Put the brace where the block really ends.
  • Looking only at the line in the error. It's the end of the script, not the mistake.
  • Deleting code until the error disappears. You'll lose work. Use formatting and bracket matching instead.

A debugging walkthrough

  1. Note which script the error is in: click the file name on the right of the error in the console.
  2. Format the whole file, and look for the first place where indentation goes wrong.
  3. Check each function, if, for and callback above that place: does each { have its }, and each callback its });?
  4. Add the missing character where the block should end, not at the end of the file.
  5. Reload the page. If a new syntax error appears, fix it the same way; one missing brace can hide another problem.

Practice

What does each give?

  1. if (x > 1) { console.log(x); (and nothing after)
  2. const a = [1, 2, 3;
  3. JSON.parse("")

Answers. (1) Unexpected end of input: the if block isn't closed. (2) Unexpected token ';': the array is still open when the browser reaches ;. (3) Unexpected end of JSON input: empty data, not a code problem.

Frequently asked questions

What does "Unexpected end of input" mean in JavaScript?

The script ended while JavaScript was still waiting for a closing }, ), ] or backtick. Something opened is never closed.

Why does the error point to the last line?

Because that's where JavaScript noticed: it read the whole script looking for the closing character. The mistake is earlier, where a block should end.

How do I find a missing bracket in VS Code?

Format the document, click brackets to see their matches, and fold functions to see where each one really ends.

Is "Unexpected end of JSON input" the same error?

Same words, different cause. That one means the JSON text was empty. Check the response or the stored value before parsing.

Why does nothing in my script run?

A syntax error stops the whole script before it starts. Fix it, and the rest of the code runs again.

A quick checklist

  1. Don't trust the line number: it's the end of the script.
  2. Format the file and look for where indentation breaks.
  3. Check every { has a }, and every callback ends with });.
  4. Check template strings have a closing backtick.
  5. With fetch or JSON.parse, check whether the data was empty.

For a method that works on any error, read how to read an error message. MDN lists JavaScript's syntax errors in its error reference.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right