SHARE
Facebook X Pinterest WhatsApp

Back by Popular Demand: More on JavaScript Variable Naming Conventions

Written By
thumbnail
Rob Gravelle
Rob Gravelle
Jan 23, 2013

The Variable Naming Conventions in JavaScript article only scratched the surface of Variable Naming conventions in JavaScript. This article introduces more in-depth info on how to make your code both more readable and maintainable.

Objects vs. Instances

While all variables are written in Camel Case (e.g. CamelCase), object identifiers have the distinction of starting with an uppercase letter, as in Account or BorderPatrolOfficer. Instances, on the other hand, should never begin with an uppercase letter.

var Account = function Account(acctNum) { //uppercase 'A'
  this.acctNum = acctNum;
  this.balance = 0.0;
  
  this.getBalance() = function() {
    return this.balance;
  }
  //etc…
};
//anAccount variable begins with a lowercase 'a'
var anAccount = new Account(111); 

function AccountHolder(name) { //uppercase 'A'
  this.name = name;
  this.accounts = [];
  
  this.addAccount(account) {
    this.accounts.add(account);
  }
  //...
}
//jimmy variable begins with a lowercase 'j'
var jimmy = new AccountHolder('Jimmy'); 

var Bank = {
  address: '99 Reynolds st.',
  accounts: [],
  openAccount: function(accountHolder) {
    this.accounts.add(new Account(accounts.length + 1));
  }
  //... 
};

Acronyms

One source of confusion for developers is names that contain an acronym. Typically, it’s best to enter the full term rather than the abbreviation, but there are cases where it would be redundant to do so, as in the case of well-known acronyms like DVD, HTML, and BYOB(!). In such instances, most choose to stick to strict camel case and only capitalize the first letter. This is especially advisable when the acronym is at the start of the word, for instance, DvdPlayer (as opposed to DVDPlayer, or the extra informative DigitalVersatileDiscPlayer!). Were you to name the instance with a lowercase first letter, the latter would give you the awkward dVDPlayer identifier.

Variable Assignment Via Setter

Whether by constructor or setter, there are a lot of differing opinions on what to call the function arguments. Observe the following variations:

function MyClass(aFoo)
{
  this.foo = aFoo;

  this.setFoo(_foo) {
    this.foo = _foo;
  }
}

The clearest way to name your constructor and setter parameters is to simply use the same name as the objet property that it sets. The this pointer distambiguates the object member from the local variable so there is no danger of encountering runtime errors:

function MyClass(foo)
{
  this.foo = foo;

  this.setFoo(foo) {
    this.foo = foo;
  }
}

The exception to the rule is where the class variable is itself local:

function MyClass(foo)
{
  var foo = foo;  //error!

  this.setFoo(foo) {
    foo = foo;   //error!
  }
}

The best solution here is to change the object attribute, not the setter parameter. Starting the variable with an underscore is a standard way to identify it as being private:

function MyClass(foo)
{
  var _foo = foo;  //error!

  this.setFoo(foo) {
    _foo = foo;   //error!
  }
}

Looping Variables

In being as descriptive as possible, eschew short forms such as act, dt, and ps. The exception here is variables used in loops such as i (for iterator) or c (for count).

for (var i=0; i

Collection Naming

Collections include objects such as Arrays and HashTables. In both cases, they are used to store multiple items. As such their names should be pluralized to reflect their multiplicity:

var accounts = [account1, account2, account3];
var cars     = {};
    cars['buick']  = new Car('Buick');
    cars['camaro'] = new Car('Camaro');
    cars['viper']  = new Car('Viper');

It is also acceptible to append the word “list” to the type name:

var accountList = [account1, account2, account3];
var carList     = {};
    carList['buick']  = new Car('Buick');
    carList['camaro'] = new Car('Camaro');
    carList['viper']  = new Car('Viper');

Boolean Variables and Methods

Whenever possible, methods that return a boolean result should start with the prefix “is”. Examples include isValidFormat(), isCanadianCitizen(), isDeclawed(). However, certain words dictate another prefix, such as usesPostOffice(), hasMoustache(). As long as the method name is posed as a yes/no question, you’re on the right track. The verb prefix satisfies the requirement that all methods start with a verb.

Ironically, boolean variables could be named the same way, except that they would overwrite the method!

var isValidFormat = isValidFormat(); //bad idea!

To avoid a naming collision, consider rearranging the wording so that isValidFormat() becomes formatIsValid or usesPostOffice() becomes postOfficeIsUsed. Or, as Yoda would say, “UsedPostOfficeIs, yes.”:

var formatIsValid = isValidFormat(); //much better
//...

if (formatIsValid) {
  //good to go…
}

Conclusion

Whether you agree with all of the conventions described here today, perhaps the most important habit you can cultivate is to be consistent in your naming standards.

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.