Open In App

JavaScript SyntaxError – Unexpected end of input

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A SyntaxError: Unexpected end of input error in JavaScript occurs when the interpreter reaches the end of script it is reading and it indicates that the code is incomplete, this error will prevent the code from running and mostly happens when a closing bracket, quote or parenthesis are missing, here's how to understand this error, its causes and how to resolve it with examples.

Understanding an error

An “Unexpected end of input” error results from unfinished code structures such as unclosed blocks, strings or functions definitions, for instance, in JavaScript, if some code is not properly enclosed and terminated then when it reaches the end of that script without finding its required closures there is a SyntaxError.

Case 1: Missing Closing Bracket

Error Cause:

In this case if we miss a closing bracket in a block of code can cause an Unexpected end of input error.

Example: In the given below example we have missed to close a bracket.

function greet() {
console.log("Hello, World!";
}
greet();

Output:

SyntaxError: Unexpected end of input

Resolution of error:

You have to ensure that all code blocks are properly closed with matching brackets.

JavaScript
function greet() {
  console.log("Hello, World!");
}
greet();

Output
Hello, World!

Case 2: Unclosed String

Error Cause:

In this case if we miss an unclosed string literal will lead to an Unexpected end of input error.

Example: In the given below example we are we have missed to close string with double quote.

let message = "Hello, World;
console.log(message);

Output:

SyntaxError: Unexpected end of input

Resolution of error:

You should make sure that all string literals are properly enclosed with matching quotes.

JavaScript
let message = "Hello, World";
console.log(message);

Output
Hello, World

Case 3: Missing Closing Parenthesis

Error Cause:

In this case if we miss a closing parenthesis in expressions or function calls can cause an Unexpected end of input error.

Example: In the given below example we are we have missed to close a bracket.

let sum = (a, b => {
return a + b;
console.log(sum(5, 10));

Output:

SyntaxError: Unexpected end of input

Resolution of error:

To avoid such kind of error you should ensure that all parentheses are properly closed.

JavaScript
let sum = (a, b) => {
  return a + b;
}
console.log(sum(5, 10));

Output
15

Case 4: Incomplete Array or Object Literal

Error Cause:

In this case if we have an incomplete array or object literal will lead to an Unexpected end of input error.

Example: In the given below example we are we have missed to close a bracket.

let numbers = [1, 2, 3;
console.log(numbers);

Output:

SyntaxError: Unexpected end of input

Resolution of error:

You have to ensure that all arrays and objects are properly closed with matching brackets or braces.

JavaScript
let numbers = [1, 2, 3];
console.log(numbers);

Output
[ 1, 2, 3 ]

Conclusion

To avoid "Unexpected end of input" errors in JavaScript, you need to confirm that each and all code structures are closed completely, look out for closing brackets, quotes, parentheses and make sure arrays as well as objects are enclosed properly, this ensures these values are well maintained within our program.


Next Article

Similar Reads