Best Ways To Name
JavaScript Variables
@richwebdeveloper
1. Use camelCase for variables and
functions
DO: let userAge = 25;
DON'T: let user_age = 25;
camelCase is the standard in JavaScript. It
improves readability and follows the language's
conventions.
2. Be descriptive but concise
DO: let isUserLoggedIn = true;
DON'T: let flag = true;
Descriptive names make your code self-
documenting, reducing the need for comments
and making it easier for others (or yourself in the
future) to understand the code's purpose.
@richwebdeveloper
3. Use intention-revealing names
DO: let elapsedTimeInDays = 7;
DON'T: let d = 7;
Clear intentions in variable names help readers
understand the variable's purpose without
needing to deduce it from the surrounding code.
4. Add prefixes for booleans
DO: let isActive, let hasPermission
DON'T: let active, let permission
Prefixes like 'is', 'has', or 'can' immediately indicate
that the variable is a boolean, improving code
clarity.
@richwebdeveloper
5. Use plural nouns for arrays
DO: let colors = ['red', 'green', 'blue'];
DON'T: let color = ['red', 'green', 'blue'];
Plural names clearly indicate that the variable
contains multiple items.
6. Use verb phrases for function
names
DO: function calculateTotal() {}
DON'T: function total() {}
Verb phrases describe actions, which is exactly
what functions do.
This makes the function's purpose immediately
clear.
@richwebdeveloper
7. Use PascalCase for classes and
constructor functions
DO: class UserProfile {}
DON'T: class userProfile {}
PascalCase is the convention for classes in
JavaScript, making them easily distinguishable
from other variables and functions.
8. Use UPPERCASE_SNAKE_CASE for
constants
DO: const MAX_ITEMS = 100;
DON'T: const maxItems = 100;
This convention clearly indicates that the value
should not be changed and is a constant
throughout the program.
@richwebdeveloper
9. Avoid single letter names
(except for loops)
DO: let currentDate = new Date();
DON'T: let d = new Date();
Single letter names are rarely descriptive enough,
except in very short loop constructs where their
purpose is clear.
10. Use searchable names
DO: const MILLISECONDS_PER_DAY =
86400000;
DON'T: const VALUE = 86400000;
Using searchable names makes it easier to find
specific variables across your codebase,
especially in larger projects.
@richwebdeveloper
Are you looking for Front-end Developer Job?
If yes, don’t miss your next opportunity. Check
the link in the bio to get the Front-end
Interview Kit and be prepared for your dream
job.