JavaScript naming conventions

Angelos Chalaris · May 19, 2020 ·

JavaScript, Variable, Cheatsheet

Variables

  • Names are case-sensitive, lowercase and uppercase are different.
  • Start variable names with a letter, use camelCase for names.
  • Variable names should be self-descriptive, describing the stored value.
  • Boolean variables are usually prefixed with is or has.

Functions

  • Names are case-sensitive, lowercase and uppercase are different.
  • Start function names with a letter, use camelCase for names.
  • Use descriptive names, usually verbs in the imperative form.
  • Common prefixes are get, make, apply etc.
  • Class methods follow the same rules.

Constant

  • Names are case-sensitive, lowercase and uppercase are different.
  • Define constants at the top of your file, function or class.
  • Sometimes UPPER_SNAKE_CASE is used, while other times plain camelCase.

Classes

  • Names are case-sensitive, lowercase and uppercase are different.
  • Start class names with a capital letter, use PascalCase for names.
  • Use descriptive names, explaining the functionality of the class.
  • Components, which are used in frontend frameworks follow the same rules.

Private

  • Prefix any variable or function with _ to show intention for it to be private.
  • As a convention, this will not prevent other parts of the code from accessing it.

Recommended snippets

  • One of the most commonly asked JavaScript interview questions is about hoisting. It's also a concept that might require some getting used to, so read our guide to learn more.

  • JavaScript variables can be declared a handful of ways. However, they're not all created equal and understanding their differences can drastically change the way you code.

  • JavaScript developers often get confused by JavaScript's variables and scope. Here's a quick guide to understanding and remembering everything related to these concepts.