JavaScript SyntaxError – Missing ‘:’ after property id

This JavaScript exception missing : after property id occurs if objects are declared using the object’s initialization syntax.

Message:

SyntaxError: Expected ':' (Edge)
SyntaxError: missing : after property id (Firefox)

Error Type:

SyntaxError

Cause of Error: Somewhere in the code, Objects are created with object initializer syntax and colon (:) is used to separates keys and values for the object’s properties, Which is not used so.

Example 1: In this example, the “=” is used in place of “:”, So the error has occurred.



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 = { 
            // invalid syntax
            key = 'value' 
        };
        document.write(JSON.stringify(GFG_Obj));
    </script>
</body>
</html>

chevron_right


Output(In console in Edge Browser):

SyntaxError: Expected ':'

Example 2: In this example, the value is missing for object’s key, So the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      // invalid syntax
      var GFG_Obj = { key; };  
      document.write(JSON.stringify(GFG_Obj));
    </script>
</body>
</html>

chevron_right


Output(In console in Edge Browser):

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.