1. Computing & Technology

$ and _ in JavaScript

From , former About.com Guide

The $ symbol and the _ symbol in JavaScript are not treated the same way as all of the other special symbols are treated. Instead JavaScript treats $ and _ as if they were letters of the alphabet.

Anywhere that an alphabetic character is allowed in JavaScript, there are 54 possivle letters that you can use there - any lowercase letter (a through z), any uppercase letter (A through Z), $ and _.

So where a variable, function, or object name is allwerd to start with a letter and to contain letters and numbers there are 54 valid values it can start with and 64 valid values that can be used for each subsequent character in the name.

Since you can define variables and functions that have only the one single letter in their name, calling a function $() is just as valid and has no more special meaning than defining a variable called i. In both cases there is a convention as to how it is expected to be used but there is nothing in the language to enforce that convention.

In the same way that i is often used as the counter for a loop (which dates back to the early days of Fortran where 'i' was the simplest variable name that defaulted to being an integer rather than a floating point number), there has been a convention develop in JavaScript to use the $() function as a shortcut way to refer to document.getElementById(). 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. Many are more sophisticated than that and can provide similar references when something other than an id is passed but in just about all of the JavaScript libraries that have been developed $('x') is equivalent to document.getElementById('x'). In all cases this is because of the way that the library itself has defined the $() function.

$ was chosen for the function name by the first of these libraries because it is a short one character function name and $ was the letter which at the time 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. Of course now with multiple libraries all providing their own version of the $() function the potential for them clashing due to them providing different functionality above and beyond the simple getElementById means that at least some of the libraries now provide the option to turn off that definition and use a longer name instead when the library is being used alongside another library which has its own slightly different implementation of $().

Of course you don't need to be using a library to be able to use $(). All you need to do to be able 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 convention has also developed regarding the use of _ as the first letter of a name. To make it clear which properties and methods of an object are intended to be private without looking to see how they are defined there is a convention in many languages that private names start with a _. This is a particularly useful convention in JavaScript since the way that you define fields as private or public is done without reference to the private and public keywords (at least it is in the versions of JavaScript that is used in web browsers - JavaScript 2.0 does allow those keywords). Since JavaScript newbies are not so familiar with the way that you define things as private or public in JavaScript this convention of starting private names with _ provides the person reading the code with information that the field is intended to be private even if it has not been defined as such.

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 _ only applies within JavaScript itself. When you test for alphabetic characters in the data they will be treated as special characters no different from any of the other special characters.

©2012 About.com. All rights reserved.

A part of The New York Times Company.