Unexpected end of input Error in JavaScript

December 8, 2022

Unexpected end of input Error in JavaScript

This guide is part of the “Common JavaScript Errors” series. It’s focused entirely on providing quick and easy solutions for JavaScript-related problems.


The "Unexpected end of input" error in JavaScript is thrown when the JavaScript interpreter encounters an unexpected end of the input it was parsing. This can happen for various reasons, such as when there is a syntax error in the code or when the interpreter is expecting to read more input but reaches the end of the file or stream before it has found the expected closing syntax.

For example, if you forget to close a string with a quotation mark or forget to close a block of code with a curly brace, you may see this error. It is important to carefully check your code for any missing or extra characters that may cause this error to be thrown.

Here are some common reasons why the "Unexpected end of input" error may occur:

  • Syntax error in the code, such as a missing closing quotation mark or curly brace;
  • The interpreter reaches the end of the input without finding the expected closing syntax;
  • The code is incomplete or invalid;
  • There is a problem with the input being parsed, such as an incorrect or unexpected character;
  • The code is attempting to access an undefined variable or object property;
  • The code is trying to call a function that has not been defined or is unavailable in the current scope.

Here is an example of code that may throw this error:

const myString = 'This is a string
console.log(myString);

In this example, the string is missing its closing quotation mark, so the interpreter will throw an "Unexpected end of input" error when it reaches the end of the input without finding the expected closing quote. To fix this error, you would need to add the closing quotation mark, like this:

const myString = 'This is a string';
console.log(myString);

Another common scenario in which this error occurs is when trying to parse an empty response from a server or an empty string using the JSON.parse() method.

The JSON.parse() method is used to convert a JSON string into a JavaScript object. For example, if you have a JSON string that represents an object, such as:

const jsonString = '{"name": "John", "age": 30}';

You can use the JSON.parse() method to convert this string into a JavaScript object like this:

const obj = JSON.parse(jsonString);

However, if you try to parse an empty string or an empty response from a server using this method, you may encounter the "Uncaught SyntaxError Unexpected end of input" error. This is because the JSON.parse() method expects to read a valid JSON string as its input, but when it encounters an empty string or an empty response, it reaches the end of the input without finding the expected closing syntax.

To fix this error, you need to ensure that the input you are trying to parse with the JSON.parse() method is a valid JSON string, or handle the case where the input is empty or invalid. In other words, you can use a try/catch block to monitor for this error directly. Like so:

const jsonString = '{"name": "John", "age": 30}';

try {
  const obj = JSON.parse(jsonString);
  console.log(obj.name);
} catch (error) {
  if (error instanceof SyntaxError && error.message.includes("Unexpected end of input")) {
    console.error("Invalid JSON string");
  } else {
    throw error;
  }
}

So, in this case, if the string was empty - the catch block would catch it.