Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Error Handling: Unexpected Token

  • Last Updated : 20 Jul, 2021

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.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :