Resolving Issues with JavaScript Variables

Problem
At runtime, you encounter one of the following error messages with regards to variables. You want to know how to debug them.

SyntaxError: Unexpected token 
SyntaxError: Unexpected number
SyntaxError: Unexpected string
SyntaxError: Unexpected reserved word
SyntaxError: missing variable 
SyntaxError: is a reserved identifier
SyntaxError: Use of reserved word ''
SyntaxError: Expected an identifier but found '' instead
SyntaxError: The use of a keyword for an identifier is invalid
SyntaxError: Expected identifier
SyntaxError: Expected string ''
SyntaxError: Expected number ''
SyntaxError: Expected an identifier but found '' instead

Solution
Evaluate your variable names in the line reported of the stack trace. Ensure you haven’t violated any of these rules:

• The variable name is a language keyword (one that’s used in the language).
• The variable name is a reserved word (one that is reserved for future versions of the language).
• The variable name is a literal value in the language (for example, true or null).
• The variable name starts with a number.
• The variable name contains spaces.

For a list of reserved words, please visit this page.

Best Practices
The following items are considered “best practice” when naming variables in JavaScript:

• Use English letters and avoid numbers.
• Use camelCasing for multiple-word variables.
• Use PascalCase (or TitleCase) when naming constructors.
• Use UPPERCASE_AND_UNDERSCORES when naming constants.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s