JavaScript SyntaxError – Unexpected end of input
Last Updated :
04 Jul, 2024
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();
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);
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));
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);
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.
Similar Reads
JavaScript SyntaxError - Unexpected string
The occurrence of a âSyntaxError: Unexpected stringâ in JavaScript happens when a programmer uses string data type in an expression, statement, or declaration where it is not appropriate, this syntax error stops the script and it may happen due to incorrect placement of strings in expressions, varia
2 min read
JavaScript SyntaxError - Unexpected number
The Unexpected number error in JavaScript occurs when the JavaScript engine encounters a number in a place where it isn't syntactically valid. This error is categorized as a SyntaxError, indicating that there's a problem with the structure of your code.MessageSyntaxError: Unexpected numberError Type
3 min read
JavaScript SyntaxError - Unexpected token
This JavaScript exceptions unexpected token occur if a specific language construct was expected, but anything else is typed mistakenly. This could be a simple typing mistake. Message: SyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target,
1 min read
JavaScript SyntaxError â Unexpected reserved word
A "SyntaxError: Unexpected reserved word" error in JavaScript is when a reserved word is used incorrectly, generally in a different identifier kind of context, this error will not allow the code to run and frequently results from using keywords as variable names, function names or by putting them at
3 min read
JavaScript SyntaxError â Unexpected template string
A JavaScript error, âSyntaxError: Unexpected template stringâ, is raised when a template string ( ``) is misused or misplaced, although these strings permit the use of embedded expressions and multi-line strings, using them wrongly would cause the syntax to break, letâs get to know what this error i
2 min read
JavaScript Error Handling: Unexpected Token
Like other programming languages, JavaScript has define some proper programming rules. Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + - var if-else var etc}. Unexpected token is similar to syntax error but more specific.Semi
3 min read
JavaScript SyntaxError: Unterminated string literal
This JavaScript error unterminated string literal occurs if there is a string that is not terminated properly. String literals must be enclosed by single (') or double (") quotes.Message:SyntaxError: Unterminated string constant (Edge)SyntaxError: unterminated string literal (Firefox)Error Type:Synt
2 min read
Unexpected token error for catch JavaScript
In this article, we will try to understand the fact that why we receive Unexpected token errors while we are dealing with try/catch in JavaScript, and with the help of certain coding examples (in JavaScript itself) we will try to see how we may be able to resolve such token error properly. Unexpecte
3 min read
JavaScript SyntaxError - Invalid regular expression flag "x"
This JavaScript exception invalid regular expression flag occurs if the flags, written after the second slash in RegExp literal, are not from either of (g, i, m, s, u, or y).Error Message on console:SyntaxError: Syntax error in regular expression (Edge) SyntaxError: invalid regular expression flag "
1 min read
JavaScript SyntaxError - Illegal character
This JavaScript exception illegal character occurs if there is an invalid or unexpected token that doesn't belong there in the code.Understanding an errorAn "Unexpected token ILLEGAL" error signifies that there is an invalid character present within the code, in certain situations, JavaScript requir
2 min read