JavaScript SyntaxError – Missing ] after element list

This JavaScript exception missing ] after element list occurs, It might be an error in array initialization syntax in code. Missing closing bracket (“]”) or a comma (“,”) also raises an error.

Message:

SyntaxError: missing ] after element list

Error Type:

SyntaxError

Cause of Error: Somewhere in the script, there is an error with the array initialization syntax. Missing closing bracket (“]”) or a comma (“,”) creates a problem.

Example 1: In this example, the “]” is missing in the array declaration.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      var arr = [1, 2,                  
      document.write(arr);
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

SyntaxError : Expected ']'

Example 2: In this example, the “,” is missing in an array declaration.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      var GFG_Obj = [{prop_1: 'val_1'} {prop_2: 'val_2'}];                      
      document.write(GFG_Obj);
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

SyntaxError : Expected ']'

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.