JavaScript Naming Conventions
Last updated:
Consistency is key.
Variables
- Variables should always start with a lowercase letter:
name
. - If the variable name is made up of multiple words, use camelCase:
firstName
instead offirst_name
. - Do not use reserved words and special characters in variable names.
Constants
- Constants should be in all uppercase letters:
PI
. - If the constant name is made up of multiple words, use underscores:
MAX_SIZE
instead ofmaxSize
.
Functions
- Function names should always start with a lowercase letter:
calculate
. - If the function name is made up of multiple words, use camelCase:
calculateTotal
instead ofcalculate_total
. - Function names should be descriptive and indicate what the function does.
Classes
- Class names should always start with an uppercase letter:
Person
. - If the class name is made up of multiple words, use PascalCase:
PersonDetails
instead ofperson_details
.
Take Away
- consistency is key.
- descriptive names are better than short names.
- camelCase for variables and functions.
- PascalCase for classes.
- UPPERCASE for constants.
- Avoid using reserved words and special characters in names.