SyntaxError: Unexpected end of input in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# SyntaxError: Unexpected end of input in JavaScript [Solved]

The "Uncaught SyntaxError: Unexpected end of input" error occurs for 3 main reasons:

  1. Forgetting a closing parenthesis, bracket or quote.
  2. Trying to parse an empty response with JSON.parse() or $.parseJSON.
  3. Getting a text/html response or no response at all from a server and trying to parse it as JSON.

uncaught syntaxerror unexpected end of input

# Make sure you don't have missing closing parenthesis, brackets, and curly braces

The most common cause of the error is forgetting a closing parenthesis, bracket or quote.

index.js
// โ›”๏ธ Uncaught SyntaxError: Unexpected end of input function sum(a, b) { return a + b; // ๐Ÿ‘†๏ธ forgot the closing curly brace if (true) { // ๐Ÿ‘†๏ธ forgot the closing curly brace const arr = ['a', 'b' // ๐Ÿ‘ˆ๏ธ forgot closing square bracket const obj = {name: 'Tom' // ๐Ÿ‘ˆ๏ธ forgot closing curly brace

You can resolve the syntax error by adding the missing parenthesis, bracket and curly brace.

index.js
function sum(a, b) { return a + b; } // ๐Ÿ‘ˆ๏ธ added closing curly brace if ('hi'.length === 2) { console.log('success') } // ๐Ÿ‘ˆ๏ธ added closing curly brace const arr = ['a', 'b', 'c'] // ๐Ÿ‘ˆ๏ธ adding closing square bracket const obj = {name: 'Tom', age: 30} // ๐Ÿ‘ˆ๏ธ added closing curly brace

add missing parenthesis bracket and curly brace

The code for this article is available on GitHub

Make sure that your code is formatted properly and that you see no typos that would cause a syntax error.

Open the console in your browser's developer tools to check on which line the error occurred.

browser console line of error

The error message shows that the SyntaxError was thrown on line 4 in the index.js file.

# Make sure you aren't trying to JSON.parse an empty string

The error also occurs if you try to parse an empty response from your server or an empty string using the JSON.parse() method.

index.js
// โ›”๏ธ Uncaught SyntaxError: Unexpected end of JSON input console.log(JSON.parse('')); console.log($.parseJSON(''));
We parsed an empty string and got the error. This can also happen if your server sends an empty response and you attempt to parse the result with JSON.parse().

To make sure you handle this scenario, you can:

  • Wrap your parsing logic in a try/catch block.
  • Make sure to return a valid JSON response from your server.
  • Remove the parsing logic from your code if you are expecting an empty server response.

You can inspect the server's response by opening your developer tools and clicking on your Network tab. If you click on the Response tab, you will see the server's response.

browser network tab response

# Using a try/catch block to handle the error

Here's an example of how to use a try/catch block to avoid the "Unexpected end of input" error when parsing.

index.js
try { const result = JSON.parse(''); console.log(result); } catch (err) { // ๐Ÿ‘‡๏ธ SyntaxError: Unexpected end of JSON input console.log('๐Ÿ‘‰๏ธ error', err); }
The code for this article is available on GitHub

If the JSON.parse function throws an error due to parsing invalid JSON, the error is passed as a parameter to the catch function where we can handle it.

If the value is already parsed, you don't have to use the JSON.parse function.

Use the typeof operator to check the type of a value.

index.js
console.log(typeof ['a', 'b', 'c']); // ๐Ÿ‘‰๏ธ object console.log(typeof 13.7); // ๐Ÿ‘‰๏ธ number

If the typeof operator returns anything other than string, then the value is not JSON and you shouldn't use the JSON.parse method.

Alternatively, you can remove the call to the JSON.parse function if you know the server's response does not contain valid JSON.

You can paste your code into an online Syntax Validator . The validator should be able to tell you on which line the error occurred.

# Conclusion

To solve the "Uncaught SyntaxError Unexpected end of input" error:

  1. Add any missing closing parenthesis, bracket or quote.
  2. Don't try to parse an empty response with JSON.parse() or $.parseJSON.
  3. Make sure your server returns the correct response type, e.g. trying to parse invalid JSON causes the error.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev