To fix SyntaxError: Unexpected token in JavaScript, we must first understand the reason for the error and then figure out how to fix it correctly. This is a fairly common error for newbies and rarely appears for veterans, but fortunately, fixing it is very simple.
Why is SyntaxError: Unexpected token in JavaScript happening?
The error SyntaxError: Unexpected token in JavaScript can be a basic typo, appearing when a statement is syntactically incorrect. Once or more characters we type in excess or missing will lead to the above error.
The error message occurs as follows:
Uncaught SyntaxError: Unexpected token
In this tutorial, I will show you how to fix this error in two cases: incorrect use of JSON.loads() and empty JSON file.
How to fix this error?
Let’s follow two steps below to fix this error.
Step 1: Find out the cause of the error
Since this error is a typo, the first step is to check where the error is and how it is, and then provide a solution.
Example 1:
var num = [1,2,4,5,7,9]; var sum = 0 ; for(let i = 0 ; i < num.length ,; i++ ) sum = sum + num[i]; console.log(sum);
Output:
Uncaught SyntaxError: Unexpected token ';' (at main.js:3:33)
In this example, my error is in line 3, position 33 in the line from the left. When I look at it, I see that I put an extra ” , ” and the program generates an error, so the error is because I marked it too much ” , “.
Example 2:
var currentDate = new Date(); var firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth, 1); var lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth + 1 , 0); if(currentDate.getDate == lastDayOfMonth.getDate())||(currentDate.getDate() == 1) console.log("This is the first or last day of the month"); else console.log("This is not the first or last day of the month");
Output:
Uncaught SyntaxError: Unexpected token '||' (at main.js:5:52)
This example shows my error in line 5, position 52 in the line from the left. After checking, I find two characters, “()” are missing to close the if condition.
Step 2: Fix error
Once we find out what caused the error, we can start fixing it.
In example 1, we see the error because I have marked an extra ” , ” so the program generates an error. To fix this error, I will do the following:
var num = [1,2,4,5,7,9]; var sum = 0 ; for(let i = 0 ; i < num.length ; i++ ) sum = sum + num[i]; console.log(sum);
Output:
28
The code seems to have not changed, but the program usually runs. That’s because I removed the error-generating agent and the ” , ” that I added. Then the program will no longer have errors and will run normally.
In example 2, we see the error because I was missing two “()” signs, so the program generates an error. To fix this error, I will do the following:
var currentDate = new Date(); var firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth, 1); var lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth + 1 , 0); if((currentDate.getDate == lastDayOfMonth.getDate())||(currentDate.getDate() == 1)) console.log("This is the first or last day of the month"); else console.log("This is not the first or last day of the month");
Output:
This is not the first or last day of the month
My program usually runs after I add the two “()” s that I missed.
Summary
SyntaxError: Unexpected token in JavaScript is a fundamental error, but to avoid reencountering it while writing the code, you should pay more attention to not redundant or redundant characters. I hope this article will help you fix the error in your program, so you won’t have to face the same problem again. Good luck.
Leave a Reply