The "Uncaught SyntaxError: Unexpected identifier" error is typically thrown as a result of a misspellt keyword, missing operator or missing comma.
Keywords in JavaScript include words such as let
, class
, function
, const
. These words have a special meaning in the language and are part of the syntax. They must be written as the language expects them, without any variance in casing or spelling.
The following example shows how the "Uncaught SyntaxError: Unexpected identifier" error can result from a misspellt keyword.
cons foo = 500; // Fix by changing cons to const
classs Foo {} // Fix by changing classs to class
functionn foo {} // Fix by changing functionn to function
Var foo = 100; // Fix by changing Var to var
The following example shows how the "Uncaught SyntaxError: Unexpected identifier" error can result from a missing comma or missing operator.
const foo = { one: 1 // Fix by adding the missing comma two: 2};
function foo(a b) {} // Fix by adding the missing comma
function foo(a, b) { return a b; // Fix by adding the missing operator}
Typically, your code editor will let you know something is wrong when writing code like the above examples. Either through syntax highlighting, as misspellt keywords won't receive the same highlighting as the correctly spellt ones or through a squiggly underline that will give you information about the issue when you hover over it.
In any case, this error is quick to solve as long as you can spot the cause, which is usually just a typo. However, if you need help finding the reason, you can also try pasting your code into an online syntax validator such as the Esprima syntax validator.
Conclusion
To fix the "Uncaught SyntaxError: Unexpected identifier" error, add any missing commas or operators and correct any misspellt keywords, such as Const
instead of const
or Class
instead of class
.