The Dollar Sign ($) and Underscore (_) in JavaScript

The Conventional Use of the $ and _ in JavaScript

Format
mla apa chicago
Your Citation
Chapman, Stephen. "The Dollar Sign ($) and Underscore (_) in JavaScript." ThoughtCo, Jan. 1, 2017, thoughtco.com/and-in-javascript-2037515. Chapman, Stephen. (2017, January 1). The Dollar Sign ($) and Underscore (_) in JavaScript. Retrieved from https://www.thoughtco.com/and-in-javascript-2037515 Chapman, Stephen. "The Dollar Sign ($) and Underscore (_) in JavaScript." ThoughtCo. https://www.thoughtco.com/and-in-javascript-2037515 (accessed October 22, 2017).
A young woman is working on her laptop
Joakim Leroy/E+/Getty Images

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events and objects.

For this reason, these characters are not treated the same way as other special symbols. Instead, JavaScript treats $ and _ as if they were letters of the alphabet.

A JavaScript identifier — again, just a name for any object — must start with a lower or upper case letter, underscore (_), or dollar sign ($); subsequent characters can also include digits (0-9). Anywhere that an alphabetic character is allowed in JavaScript, 54 possible letters are available: any lowercase letter (a through z), any uppercase letter (A through Z), $ and _.

The Dollar ($) Identifier

The dollar sign is commonly used as a shortcut to the function document.getElementById(). Because this function is fairly verbose and used frequently in JavaScript, the $ has long been used as its alias, and many of the libraries available for use with JavaScript create a $() function that references an element from the DOM if you pass it the id of that element.

There is nothing about $ that requires it to be used this way, however. But it has been the convention, although there is nothing in the language to enforce it.

The dollar sign $ was chosen for the function name by the first of these libraries because it is a short one-character word, and $ was least likely to be used by itself as a function name and therefore the least likely to clash with other code in the page.

Now multiple libraries are providing their own version of the $() function, so many now provide the option to turn off that definition in order to avoid clashes.

 

Of course,  you don't need to use a library to be able to use $(). All you need to substitute $() for document.getElementById() is to add a definition of the $() function to your code as follows:

function $(x) {return document.getElementById(x);}

The Underscore _Identifier 

A convention has also developed regarding the use of _, which is frequently used to preface a name of an object's property or method that is private. This is a quick and easy way to immediately identify a private class member, and it is so widely used, that almost every programmer will recognize it.

This is particularly useful in JavaScript since defining fields as private or public is done without the use of the private and public keywords (at least this is true in the versions of JavaScript used in web browsers — JavaScript 2.0 does allow these keywords).

Note that again, as with $, the use of _ is merely a convention and is not enforced by JavaScript itself. As far as JavaScript is concerned, $ and _ are just ordinary letters of the alphabet.

Of course, this special treatment of $ and _ applies only within JavaScript itself. When you test for alphabetic characters in the data, they are treated as special characters no different from any of the other special characters.

Format
mla apa chicago
Your Citation
Chapman, Stephen. "The Dollar Sign ($) and Underscore (_) in JavaScript." ThoughtCo, Jan. 1, 2017, thoughtco.com/and-in-javascript-2037515. Chapman, Stephen. (2017, January 1). The Dollar Sign ($) and Underscore (_) in JavaScript. Retrieved from https://www.thoughtco.com/and-in-javascript-2037515 Chapman, Stephen. "The Dollar Sign ($) and Underscore (_) in JavaScript." ThoughtCo. https://www.thoughtco.com/and-in-javascript-2037515 (accessed October 22, 2017).