Skip to main content

How to Resolve the "SyntaxError: Unexpected end of input" Error in JavaScript

The SyntaxError: Unexpected end of input is a common error in JavaScript that occurs when the JavaScript engine is parsing your code and reaches the end of the file or script block before it found the expected closing character for a statement. In simple terms, it means you've started something (like a function, a string, or an object) but never properly finished it.

This guide will walk you through the most common causes of this syntax error, such as missing curly braces, parentheses, or quotes, and show you how to find and fix them.

The Core Problem: Incomplete Code Structures

The JavaScript parser reads your code from top to bottom. When it encounters an opening character like {, (, or ', it expects to find its corresponding closing character (}, ), ') later in the code. If it reaches the end of the input without finding it, it doesn't know how to complete the statement and throws this error.

Cause 1 (Most Common): Missing Curly Braces }

This is the most frequent cause of the error. You have an if statement, a for loop, a function, or an object literal where you have forgotten the closing curly brace.

Example of problem:

// Problem: The `myFunction` block is missing its closing curly brace.
function myFunction(name) {
if (name) {
return `Hello, ${name}`;
}
// <-- The function's closing '}' is missing here.
note

When the parser reaches the end of the file, it's still inside the myFunction definition and throws the error because the function was never properly closed.

Solution: ensure every opening brace { has a corresponding closing brace }. Modern code editors with code formatting (like Prettier) and syntax highlighting are extremely helpful in automatically finding and fixing these issues.

// Correct: The function block is now properly closed.
function myFunction(name) {
if (name) {
return `Hello, ${name}`;
}
} // <-- Closing brace added.

Cause 2: Missing Parentheses )

This error can also occur if you forget to close a parenthesis, especially in complex function calls or mathematical expressions.

Example of problem:

// Problem: The `console.log` statement is missing its closing parenthesis.
console.log('This is a test string';

Solution: add the missing closing parenthesis.

// Correct: The parenthesis is now closed.
console.log('This is a test string');

Cause 3: Missing Quotes (' or ")

If you start a string literal with a quote but forget to close it, the parser will think the rest of your file is part of that string. When it reaches the end of the file without finding a closing quote, it will throw the error.

Example of problem:

// Problem: The string is never closed.
const myMessage = 'This is an unterminated string that goes on and on...

Solution: add the missing closing quote.

// Correct: The string is now properly terminated.
const myMessage = 'This is an unterminated string that goes on and on...';

This is a specific variant of the same error, but it occurs when you call JSON.parse() on an empty or incomplete string.

Example of problem:

// Problem: An empty string is not valid JSON.
const jsonString = '';
JSON.parse(jsonString);

Error Output:

Uncaught SyntaxError: Unexpected end of JSON input
note

This also happens frequently when a fetch request expects a JSON response but the server returns an empty body. ::

Solution: before parsing, ensure the string is not empty. When using fetch, check the response body before calling .json().

const jsonString = '';
let data;

if (jsonString) {
try {
data = JSON.parse(jsonString);
} catch (e) {
console.error('Invalid JSON');
}
}

How to Debug the Error

  • Check the Console: The error message in your browser's developer console will usually give you a line number that is at or near the end of your file, which is a strong clue that something was left open.
  • Use a Linter/Code Editor: Tools like ESLint and code editors like VS Code are invaluable. They will almost always highlight the line with the missing character with a red squiggly underline, immediately showing you where the problem is.
  • Check Your Brackets: In your editor, click on an opening { or (. A good editor will highlight its matching closing partner. If it doesn't, or if it highlights one you didn't expect, you've found your problem.

Conclusion

The SyntaxError: Unexpected end of input is always caused by an incomplete statement in your code.

To solve it, carefully check your code for:

  • A missing closing curly brace } for a function, if block, loop, or object.
  • A missing closing parenthesis ) for a function call or expression.
  • A missing closing quote (' or ") for a string.
  • An attempt to JSON.parse() an empty string.

Using a modern code editor with a linter is the best way to prevent and quickly identify these common syntax errors.