JavaScript Unexpected End Of Input Error (Resolved)

When you get JavaScript unexpected end of input error, it means that there is something wrong with the syntax of your code. This error can be caused by a missing parenthesis, brackets or quotes. It may even be caused if you are trying to use JSON.parse() on an empty string.

To fix this error, check your code for any missing symbols or characters, and make sure that all your curly braces match up. If you are using JSON.parse(), make sure that the string you are trying to parse is not empty.

javascript unexpected end of input error

How Does The JavaScript unexpected end of input Error Occur?

The unexpected end of input error can occur dur to one of the following reasons –

1. A Missing Parenthesis

If you have a JavaScript code like this –

function myFunction(a, b){

console.log(a + b);

}

And you forget to add the closing parenthesis, then it will result in an unexpected end of input error –

function myFunction(a, b){

console.log(a + b);

You will get a ”unexpected end of input” error in this case due to the missing parenthesis.

2. A Missing Bracket

If you are using square brackets in your code and forget to close them, it will result in an unexpected end of input error. For example –

var arr = ["one", "two", "three"];

console.log(arr);

This code will work fine, but if you forget to add the closing square bracket, then it will result in an error –

var arr = ["one", "two", "three" //SyntaxError: Unexpected end of input

3. A Missing Quote

Similarly, If you forget to add a closing quotation mark in your code, it will also result in an unexpected end of input error.

4. Trying To Use JSON.parse() On An Empty String

Another common reason for this error is trying to use JSON.parse() on an empty string. For example –

var jsonStr = '{}'; // valid json string

var obj = JSON.parse(jsonStr); // parsed successfully

console.log(obj); // {}

But if you try to parse an empty string using JSON.parse(), it will result in an error –

var jsonStr = "; // empty string

var obj = JSON.parse(jsonStr); // SyntaxError: Unexpected end of JSON input

This is because an empty string is not a valid JSON string.

So these are some of the common reasons for the “unexpected end of input” error in JavaScript. Now let’s see how to fix this error.

How To Fix The JavaScript unexpected end of input Error?

The fix for this error depends on the reason why it occurred in the first place. Let’s take a look at each case one by one –

1. Use A JS Validator

If you are not sure where the error is occurring in your code, then the best way to find it is by using a JavaScript validator. A JS validator will check your code for any syntax errors and tell you exactly where they are. There are many online JavaScript validators available, such as JSLint, ESLint, and JSHint.

2. Check Your Code For Any Missing Symbols

If you are getting an unexpected end of input error, the first thing you should do is check your code for any missing symbols or characters. This includes parenthesis, brackets, and quotation marks. Make sure that all your curly braces match up as well.

3. Use try/catch Statement When Trying To Use JSON.parse()

If you are getting this error when trying to use JSON.parse(), then make sure that the string you are trying to parse is not empty. You can do this by using a try/catch statement as shown below –

try {

var obj = JSON.parse(jsonStr);

console.log(obj); // {}

} catch (e) {

console.log(e); // SyntaxError: Unexpected end of JSON input

}

This way, if the jsonStr variable is empty, it will throw an error and get caught by the catch statement. Otherwise, it will be parsed successfully.

4. Use A Pre-Processor

If you are getting the unexpected end of input error due to missing symbols or characters, then you can use a pre-processor to automatically add them for you. For example, if you are using the Vue.js framework, you can use the vue-template-eslint-plugin which adds missing symbols automatically.

Conclusion

In this article, we saw what the unexpected end of input error is and how to fix it in JavaScript. To recap, here are the four main reasons for this error –

1. Forgetting to add a closing parenthesis in a function.

2. Forgetting to add a closing square bracket.

3. Forgetting to add a closing quotation mark.

4. Trying to use JSON.parse() on an empty string.

I hope you found this article helpful. If you have any questions, feel free to post them in the comments section below and I will be happy to answer them.

Happy Coding! 🙂

Leave a Comment