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 identifier
An âUnexpected identifierâ error occurs when you have put an identifier where it is not supposed to be in your JavaScript code, it results in a syntax error that makes the interpreter fail to read the code, mostly, it happens because of incorrect identification or location in relation to expressions
3 min read
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 Typ
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 - Return not in function
This JavaScript exception return (or yield) not in function occurs if a return/yield statement is written outside the body of the function. Message: SyntaxError: 'return' statement outside of function (Edge)SyntaxError: return not in function (Firefox)SyntaxError: yield not in function (Firefox)Erro
2 min read
JavaScript SyntaxError - Missing ) after condition
This JavaScript exception missing ) after condition occurs if there is something wrong with if condition. Parenthesis should be after the if keyword. Message: SyntaxError: Expected ')' (Edge)SyntaxError: missing ) after condition (Firefox)Error Type: SyntaxErrorCause of Error: Somewhere in the code
2 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:Syn
2 min read