unexpected end of input error in JavaScript

 

"Uncaught Syntax Error Unexpected end of input" error occurred mainly due to following reasons 

 

Problems

const fun = function() {
    console.log("some output");
 // missing curly brace

const fun2 = function() {
    console.log("some output");
; // added semicolon insted of curly brace

const javascripArray = [1, 2 //  missing square bracket

const javascripObject = {key: "value" // missing curly brace

Solution 

const fun = function() {
    console.log("some output");
} // added curly brace

const fun2 = function() {
    console.log("some output");
} // added curly brace  insted of semicolon

const javascripArray = [ 1, 2 ];//  added square bracket

const javascripObject = {key: "value"}; // added curly brace

 

The "Uncaught Syntax Error Unexpected end of input" also occurs if you trying to parse an invalid JSON string. You should return accurate JSON string from your server or you can also use try catch block

 

 

check if a string is a valid JSON string

try {
  resposne = JSON.parse('{"name":"David","age":54');
}
catch(err) {
  resposne = {};
}

Tags:

Share:

Related posts