Naming Conventions (Rules) for JavaScript Variables

The following are rules (not guidelines) for naming a JavaScript variable:

  • Variable names can only consist of alpha-numeric characters (the letters a to z and the numbers 0 to 9), underscores (_) or a dollar sign $.
  • Variable names cannot start with a number.
  • Variable names must start with a letter, dollar sign ($) or an underscore (_).
  • Variable names cannot contain spaces.
  • Variable names cannot contain certain reserved keywords, such as Javascript, true, this and many more.

Reserved JavaScript keywords

Regarding reserved keywords, if you try to name a variable any of the reserved JavaScript keywords, you’ll get an error similar to this:

let this = "hey you"
// Uncaught SyntaxError: Unexpected token 'this'

Case sensitivity

In JavaScript, variables are case sensitive. These two variables are pronounced the same, but one contains an upper case, the other is pure lowercase:

  • helloThere
  • hellothere

In JavaScript world, that makes them two different variables. So always pay attention to lower and uppercase letters when you declare and reference variables in JavaScript.

You can start a variable with an uppercase letter, but the most common pattern you see in vanilla JavaScript is called camelCase, which is when the first letter of the first word in a variable is lowercase, and then the proceeding words have their first letter uppercase, like this:

let aGoodRuleOfThumb

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊

Kofi

Share & Discuss on