JS unexpected end of input – Uncaught SyntaxError: Unexpected end of input Error in JavaScript Solution

JS unexpected end of input: The error “Uncaught SyntaxError: Unexpected end of Input” arises for three reasons mostly:

  • Missing a closing parenthesis, bracket, or quote.
  • Using JSON.parse() or $.parseJSON to parse an empty response.
  • Receiving a text/Html response or no response at all from a server and attempting to parse it as JSON.

Below are the examples where this “Uncaught SyntaxError: Unexpected end of Input” occurs.

Uncaught SyntaxError: Unexpected end of input Error in JavaScript Solution

Method #1: Forgetting to close parenthesis, bracket, or quote

Example1:

// Solving Uncaught SyntaxError: Unexpected end of input Error
// Create a function say 'multiply' which accepts two numbers
// as arguments and perform multiplication operation
function multiply(x, y) {
  // Multiply the given two numbers and return their result
  return x*y;

// Here the function is not closed with the curly braces
// we may forget to enclose the brackets sometimes which leads to 
// Uncaught SyntaxError: Unexpected end of input Error

Output:

return x*y;
^

SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Explanation:

Here the function is not enclosed with the curly braces, leading to the
Uncaught SyntaxError: Unexpected end of input Error

Example2:

//Check if the condition is true using the if conditional statement
if (true) {
    
//Here we forgot to close the curly brace.

Output:

if (true) {
^

SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Explanation:

Here the if conditional statement is not enclosed with the curly braces.
Sometime we may forget these small things which leads to 
Uncaught SyntaxError: Unexpected end of input Error

Example3:

//Here the array is not enclosed with the square bracket
const array = ['x', 'y', 'z'

Output:

const array = ['x', 'y', 'z' 
^^^

SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Example4:

//Similarly we may forget to enclose the curly braces while creating
// an object. 
//Hence SyntaxError: Unexpected end of input occurs
const object = {website: 'Btechgeeks'

Output:

const object = {website: 'Btechgeeks'
^^^^^^^^^^^^

SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Method #2: Using JSON.parse() or $.parseJSON to parse an empty response.

Javascript unexpected end of input: The “Uncaught SyntaxError Unexpected end of input” error also arises when you use the JSON.parse() function to parse an empty response from your server or an empty string.

Example1:

// Here we are using the JSON.parse() function to parse 
// an emptyresponse 
// Hence, SyntaxError: Unexpected end of JSON input occurs
console.log(JSON.parse(''));

Output:

undefined:1

SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.<anonymous> (/tmp/Aff2Utqlg8.js:4:18)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Example2:

console.log($.parseJSON(''));

Output:

console.log($.parseJSON(''));
^

ReferenceError: $ is not defined
at Object.<anonymous> (/tmp/Aff2Utqlg8.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Explanation:

We encountered an error when parsing an empty string. 
This can also happen if your server returns an empty response and 
you try to parse it with JSON.parse.

To handle this case, we can use the following methods:

  • In a try/catch block, wrap your parsing logic.
  • Ensure that your server returns a valid JSON response
  • If you expect an empty server response, remove the parsing logic from your code.
// Using try-catch blocks to handle the 
// SyntaxError: Unexpected end of JSON input while passing
// an empty string to JSON.parse
try {
  const rslt = JSON.parse('');
  // printing result on the console
  console.log(rslt);
} 
catch (error) {
  // Handling the SyntaxError: Unexpected end of JSON input
  // inside the catch block
  console.log(' SyntaxErro', error);
}

Explanation:

Unexpected end of input javascript: If the JSON.parse function returns an error as a result of parsing invalid JSON, the error is passed as a parameter to the catch function, where it can be handled.

If you know the server’s response does not include valid JSON, you can delete/remove the call to the JSON.parse function.

Handling the Uncaught SyntaxError: Unexpected end of input Error in Brief

To resolve the “Uncaught SyntaxError Unexpected End of Input” error, follow these steps:

  • Close any missing parenthesis, bracket, or quote.
  • Don’t use JSON to parse an empty response. $.parseJSON or parse()
  • Check that your server gives the correct response type; for example, attempting to parse invalid JSON results in an error.