Uncaught SyntaxError: Unexpected token <

This is a common error in JavaScript, and it is hard to understand at first why it happens. But if you bear with me and remember that Bugs are a good thing you will be on your way in no time.


The JavaScript file you are linking to is returning 404 page. In other words, the browser is expecting JavaScript but it is returning HTML results.

Here is a simple example that may cause this error.

// including jquery.js
// this script is myapp.js

// Get the json data instead of making a request
    // Stop the default behaviour.
    var jsonUrl = this.href + "&json=1";

        url: jsonUrl,
        type: "json",
        method: "get",

        success: function(data){
            // do something with the data


In the example above, when the user clicks on a link an ajax request is triggered to return json data. If the json data is returned correctly, everyone is happy and move on. But if it doesn't, well we have to fix it. In situations like this, it's often common to see the error:

Uncaught SyntaxError: Unexpected token <

Don't run to stackoverflow right away. What the interpreter is telling us is that it found a character it was not expecting. Here the interpreter was expecting json, but it received < or HTML. If you check the response on your network developer tab, you will see that the response is HTML.

Another way you can get this error is if you add a script tag and have the src return HTML:

<script src="index.html"></script>

All it means is that the interpreter is expecting JavaScript or JSON and you are feeding it HTML/XML. If you don't think you are returning HTML, check if you are not getting a 404.

<!DOCTYPE html>

How do I fix it?

Check the src path to your JavaScript to make sure it is correct. If you are making an Ajax request also check the path. Either the path is incorrect, or the file doesn't exist.


jardenilson da silva :

boa tarde estou com um script e quando executo aparece o erro:Uncaught SyntaxError: token inesperado tem como solucionar? pois preciso muito deste scripr

Ibrahim :

Hi @Jardenilson

The problem is that URL you are accessing is not a script. It is returning HTML. Most probably a page that is not found 404. So try to fix the url and it should work


<script src="hello.js"></script>
             ^------------------- does not exist.

I hope this helps.

Obrigado, boa sorte

Ajay S :

Thanks a lot for the article. it helped a lot

Ibrahim :

You are welcome @Ajay

Daijobou :

I have checked everything but still the error was there and i can't get it working.

Christian :

Thank you so much. You just saved me the hassle of trying to figure out my bug

usra :

      <link rel="styleSheets" href="style.css">
      <div id="searchContainer">
     <input class"searchControl" type="text" placeholder="City name or Zipcode" id="searchInput">
     <button class"searchControl" id="searchbtn"> Search </button>
      <div id="weatherContainer">
     <div id="weatherDescription">
        <h1 id="cityHeader"></h1>
        <div id="weatherMain">
           <div id="temperature"></div>
           <div id="weatherDescriptionHeader"></div>
           <div> <img id="documentIconImg "></div>
        <div id="windspeed" class="bottomDetails"></div>
        <div id="humidity" class="bottomDetails"></div>
        <!--now work on js-->
      <script src="script.js"></script>

or script.js file code is also here:

Let appID = '905925d53d24566c39eef30036b22d33';
Let units = 'imperial';
Let searchMethod = 'Zip';

function searchWeather(searchTerm) {
    fetch(`${searchMethod}=${searchTerm}&appID=${appID}&units=${units}`).then(result => {
    return result.json();
    }).then(result => {
    // fetch api a url bcz its a 
    //call to get information from the API. This link is a queue so searchMethod(a parameter)= searchTerm (parameter value) 
    //wait until we get the info from url/server we use ".then(result=>" 
    //and next converting into json next parse the info which we get from server in init 

function init(resultFromServer) {
document.getElementById('searchbtn').addEventListener('click', () => {
    let searchTerm = document.getElementById('searchInput').value;
    if (searchTerm)

Ibrahim :

@Usra this is not the best way to get help.

If you read the article, you'll see that you may have an error in your path for script.js. Use an absolute path like:


Start with a forward slash / to start at the root of your website.

Dave :

You just saved my life!!!! I had a bug that I NEVER would have found without this. Thank you....

Let's hear your thoughts

For my eyes only