Javascript – Uncaught SyntaxError: Unexpected end of input line 1

javascriptjquerysyntaxsyntax-error

Hi there I am trying to figure out what is going wrong. I am getting the error

Uncaught SyntaxError: Unexpected end of input in line 1

which is

 var itemList = new Array();

A snippet of the code is included

var itemList = new Array();

$( document ).ready(function(){
var $newItem = $('#newItem');
var $itemList =$('#itemList');
var itemTouchStart;
var itemTouchEnd;
var itemTouchStartX;
var itemTouchEndX;

if( window.localStorage) //when application starts check if there is data
{
    itemList = JSON.parse(window.localStorage.getItem('itemList'));
}

if(null !== itemList)
{
    for (i=0;i<itemList.length; i++)
    {
        var itemNew = '<li data-key="'+ itemList[i].key +'"><span>' 
+itemList[i].item +'</span></li>';
        $itemList.append(itemNew);
    }

}
else
{
    itemList = new Array();
}

Best Answer

The line number of the error is wrong. The problem is at the end, you never close the function you're passing into ready or the call to it. Add }); at the end.

If that's missing only because you quoted a

...snippet of the beginning of the code...

...then the answer is that there's nothing wrong with line 1 that's apparent from your question. Weird errors like that can sometimes be down to invisible characters in the source, but I would expect to see an illegal character error rather than unexpected end of input. Unexpected end of input is pretty much what it says: You have a control structure of some kind open and haven't closed it when the parser reaches the end of the code text.


I find the Meteor JavaScript parser page is quite handy for diagnosing syntax errors.