[javascript] JavaScript error (Uncaught SyntaxError: Unexpected end of input)

I have some JavaScript code that works in FireFox but not in Chrome or IE.

In the Chrome JS Console I get the follow error:

"Uncaught SyntaxError: Unexpected end of input".

The JavaScript code I am using is:

<script>
 $(function() {
 $("#mewlyDiagnosed").hover(function() {
    $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
 }, function() {
    $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
 });
</script>

It says the error is on the last line which is });

This question is related to javascript jquery syntax syntax-error indentation

The answer is


In my case, I was trying to parse an empty JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");

I also got this error pointing to the end of the last script block on a page, only to realize the error was actually from clicking on an element with a onclick="pagename" instead of onclick="window.location='pagename'". It's not always a missing bracket!


I got this since I had a comment in a file I was adding to my JS, really awkward reason to what was going on - though when clicking on the VM file that's pre-rendered and catches the error, you'll find out what exactly the error was, in my case it was simply uncommenting some code I was using.


I got this error when I was trying to write a javascript bookmarklet. I couldn't figure out what was causing it. But eventually I tried URL encoding the bookmarklet, via the following website: http://mrcoles.com/bookmarklet/ and then the error went away, so it must have been a problem with certain characters in the javascript code being interpreted as special URL control characters.


http://jsbeautifier.org/ is helpful to indent your minified JS code.

Also, with Google Chrome you can use "pretty print". See the example screenshot below showing jquery.min.js from Stack Overflow nicely indented right from my browser :)

enter image description here


This error is mainly caused by empty returned ajax calls, when trying to parse an empty JSON.

To solve this test if the returned data is empty

$.ajax({
    url: url,
    type: "get",
    dataType: "json",
    success: function (response) {

        if(response.data.length == 0){
            // EMPTY
        }else{
            var obj =jQuery.parseJSON(response.data);
            console.log(obj);
        }
    }
});

I think it could be almost any javascript error/typing error in your application. I tried to delete one file content after another and finally found the typing error.


In my case, it was caused by a missing (0) in javascript:void(0) in an anchor.


In my case, it ended up being a simple double quote issue in my bookmarklet, remember only use single quotes on bookmarklets. Just in case this helps someone.


Formatting your code a bit, you have only closed the inner hover function. You have not closed the outer parts, marked below:

$(// missing closing)
 function() { // missing closing }
     $("#mewlyDiagnosed").hover(
        function() {
            $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
        }, 
        function() {
            $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
        });

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to syntax

What is the 'open' keyword in Swift? Check if returned value is not null and if so assign it, in one line, with one method call Inline for loop What does %>% function mean in R? R - " missing value where TRUE/FALSE needed " Printing variables in Python 3.4 How to replace multiple patterns at once with sed? What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript? How can I fix MySQL error #1064? What do >> and << mean in Python?

Examples related to syntax-error

Uncaught SyntaxError: Unexpected token u in JSON at position 0 How to solve SyntaxError on autogenerated manage.py? Checking whether the pip is installed? (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape How can you print multiple variables inside a string using printf? Unexpected token < in first line of HTML Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL 'Syntax Error: invalid syntax' for no apparent reason How can I fix MySQL error #1064? Notice: Trying to get property of non-object error

Examples related to indentation

How to indent/format a selection of code in Visual Studio Code with Ctrl + Shift + F Python IndentationError unindent does not match any outer indentation level How to change indentation mode in Atom? How to set HTML Auto Indent format on Sublime Text 3? "Expected an indented block" error? Indent starting from the second line of a paragraph with CSS Indentation Error in Python How to fix/convert space indentation in Sublime Text? How to make the tab character 4 spaces instead of 8 spaces in nano? IndentationError: unexpected unindent WHY?