JavaScript Error Handling: Unexpected Token
Last Updated :
28 Apr, 2025
Like other programming languages, JavaScript has define some proper programming rules. Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + - var if-else var etc}. Unexpected token is similar to syntax error but more specific.Semicolon(;) in JavaScript plays a vital role while writing a programme.
Usage: To understand this we should know JavaScript also has a particular syntax like in JavaScript are concluded by a semi-colon(;) and there are many rules like all spaces/tabs/newlines are considered whitespace. JavaScript code are parsed from left to right, it is a process in which the parser converts the statements and whitespace into unique elements.
- Tokens: All the operator (+, -, if, else...) are reserved by the JavaScript engine.So, it cannot be used incorrectly .It cannot be used as part of variable names.
- Line terminators: A JavaScript code should end with semi-colon(;).
- Control characters: To control code, it is important to maintain braces({ }) in a code. It is also important to defines the scope of the code.
- Comments: A line of code written after // is a comment. JavaScript ignores this line.
- Whitespace: It is a tab/space in code.Changing it does not change the functionality of the code.
Therefore JavaScript code is very sensitive to any typo error. These examples given below explain the ways that unexpected token can occur.
Example 1: It was either expecting a parameter in myFunc(mycar, ) or not, .So it was enable to execute this code.
javascript
<script>
function multiple(number1, number2) {
function myFunc(theObject) {
theObject.make = 'Toyota';
}
var mycar = {
make: 'BMW',
model: 'Q8-7',
year: 2005
};
var x, y;
x = mycar.make;
myFunc(mycar, );
y = mycar.make;
</script>
Output:
Unexpected end of input
Example 2: An unexpected token ', 'occurs after i=0 which javascript cannot recognize.We can remove error here by removing extra.
javascript
<script>
for(i=0, ;i<10;i++)
{
document.writeln(i);
}
</script>
Output:
expected expression, got ', '
Example 3: An unexpected token ')' occur after i++ which JavaScript cannot recognize.We can remove error here by removing extra ).
javascript
<script>
for(i=0;i<10;i++))
{
console.log(i);
}
</script>
Output
expected expression, got ')'
Example 4: At end of if's body JavaScript was expecting braces "}" but instead it got an unexpected token else.If we put } at the end of the body of if.
javascript
<script>
var n = 10;
if (n == 10) {
console.log("TRUE");
else {
console.log("FALSE");
}
</script>
Output
expected expression, got keyword 'else'
Similarly, unnecessary use of any token will throw this type of error. We can remove this error by binding by following the programming rules of JavaScript.
Similar Reads
Unexpected token error for catch JavaScript In this article, we will try to understand the fact that why we receive Unexpected token errors while we are dealing with try/catch in JavaScript, and with the help of certain coding examples (in JavaScript itself) we will try to see how we may be able to resolve such token error properly. Unexpecte
3 min read
JavaScript SyntaxError â Unexpected end of input A SyntaxError: Unexpected end of input error in JavaScript occurs when the interpreter reaches the end of script it is reading and it indicates that the code is incomplete, this error will prevent the code from running and mostly happens when a closing bracket, quote or parenthesis are missing, here
3 min read
JavaScript SyntaxError - Unexpected token This JavaScript exceptions unexpected token occur if a specific language construct was expected, but anything else is typed mistakenly. This could be a simple typing mistake. Message: SyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target,
1 min read
Javascript Error and Exceptional Handling In JavaScript, error and exception handling allows you to manage unexpected issues that occur during the execution of your code. By using tools like try, catch, throw, and finally, you can handle errors easily and prevent the program from crashing. This enables you to provide meaningful error messag
6 min read
JavaScript Error.prototype.fileName Property The fileName is a non-standard property that contains the path of the file that raised this error. It is recommended to not use this property on production sites facing the web, as it may not work for every user. If called from the Firefox Developer Tools, "debugger eval code" is returned. This prop
2 min read
JavaScript SyntaxError - Unexpected string The occurrence of a âSyntaxError: Unexpected stringâ in JavaScript happens when a programmer uses string data type in an expression, statement, or declaration where it is not appropriate, this syntax error stops the script and it may happen due to incorrect placement of strings in expressions, varia
2 min read