To fix the SyntaxError: Unexpected end of input in JavaScript, check your whole code and find out if are you missing any brackets or parentheses. If this is the case, end each module that must be closed with proper parentheses.
There are specific reasons why this error SyntaxError: Unexpected end of input occurs. Some common reasons are: Missing closing parentheses, Quotes, or brackets, and When we try to parse an empty JSON with JSON.parse() or $.parseJSON.
- Missed closing parentheses, Quotes, or brackets.
- When we try to parse an empty JSON with JSON.parse() or $.parseJSON.
You will get the error when you forget to end your code with proper parentheses, quotes, or brackets.
Example
The following code will generate SyntaxError: Unexpected end of input.
// Uncaught SyntaxError: Unexpected end of input
function sum(a, b) {
return a + b;
//forgot closing curly brace
if (true) {
// forgot closing curly brace
const array = [1, 2 // forgot closing square bracket
const object = { name: 'Diwakar' // forgot closing curly brace
For example, if you run the above file, you will get the following error.
Uncaught SyntaxError: Unexpected end of input
The Error “Uncaught SyntaxError: Unexpected end of input” also gets generated when you try to parse a JSON that doesn’t have any data using JSON.parse() function or using $.parseJSON.
// Uncaught SyntaxError: Unexpected end of JSON input
console.log(JSON.parse(''));
console.log($.parseJSON(''));
As we discussed earlier, to solve the SyntaxError, check your whole code and find out are you missing any brackets or parentheses.
To debug your code, you can use an inbuilt debugger of JavaScript. If you don’t know how to use the debugger in JavaScript, you can read our article.
If you don’t want to come across this error because of the JSON parsing, you should take care of the below points.
- Wrap your parsing code inside the try/catch block.
- Check whether your JSON is returning a valid response from the server or not.
- If you want an empty response from the server, then no need to write parsing logic on your code. Instead, remove that from the code.
That’s it for this tutorial.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.