JavaScript Best Practices

JavaScript Best Practices — Variable Naming Conventions

Spread the love

JavaScript is a very forgiving language. It’s easy to write code that runs but has issues in it.

In this article, we’ll look at best practices for naming JavaScript variables.

When You Should Have a Naming Convention

We need some naming conventions so that everyone working on the project are on the same page.

Also, we run a program over to another person for changes, they’ll also be on the same page.

It also makes reviewing things easier as we don’t have to find out what something means.

When a program is so big that we can’t hold them in brains, then having conventions reduce the cognitive load of our brains.

Informal Naming Conventions

Naming conventions vary between languages, but there’re some that apply to language.

Variable names and functions names are both camelCase except for classes constructor functions, which are named in PascalCases.

Objects are in camelCase.

To make identifying global variables easier, we can put a g prefix before the global variable.

Member variables are named the same as other variables in JavaScript.

But we may put a _ before the member variable of a class to distinguish them from other variables.

Other Conventions for Names in JavaScript

Index variables are named with short names like i and j .

Constants are named with all caps with underscores in between words.

get and set are used for accessor methods.

Property names of objects are also named with camel case.

Parameters of functions are also camel case as with variables.

Standardized Prefixes

We may want to prefix our variables names to make identifying them easier.

To do that, we may add semantic prefixes to some variables to make them easier to understand.

For instance, we may have c for count, first for the first element of an array, g for a global variable, i for array index, and last for the last element of an array.

The m prefix may be used for class instance variables. max for the maximum of a set of numbers, min is the minimum number from an array.

Advantages of Standardized Prefixes

Standardized prefixes give us all the advantages of standard naming conventions,

We don’t have to dig deep to find what something means.

The distinctions between prefixes like min , first , last and max are helpful.

Creating Short Names That Are Readable

If we abbreviate variable names, then we should make sure that they’re abbreviations that everyone understands.

There are many ways to abbreviate variable names.

We can use standard abbreviations.

Nonleading vowels should be removed from abbreviations. For instance, apple becomes appl .

Articles like and , or , the , etc. should be removed.

The first letter or first few letters should be used.

Truncating words consistently may also be used for naming variables.

Using the first and last letters of each word is another possibility.

Every significant word in the name can also be used.

Useless suffixes can also be removed.

These are all possible ways that we can abbreviate variable names.

Phonetic Abbreviations

We can also use phonetic abbreviations. However, it’s not very clear so it’s probably a bad idea to use them.

Good Practices When Abbreviating Variable Names

We shouldn’t abbreviate by removing one character from a word.

Since it’s only one character shorter, it doesn’t help us too much.

Abbreviating consistent is also a good idea. We should keep some conventions to remove our cognitive load.

Creating names that we can pronounce is also good.

Combinations that may result in misreading or mispronunciation also isn’t good. So sEnd is better than send , for example.

A thesaurus can help us resolve naming collisions if we do run into them.

If we have short names, we may want to document them so that people can understand what those names stand for.

A project-level document for names that people might not understand is also a good idea to have.


To make sure that everyone understands the names that we name, we got to name them clearly.

If we abbreviate, then we got to stick to a convention for abbreviation and keep a list of abbreviations if needed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *