Dauris Little

  • About
  • Dauris’s Portfolio
  • Blogging Lyf
  • Contact

The ($) and (_) within JavaScript

The ($) and (_) within JavaScript

by Dauris / Friday, 04 October 2019 / Published in blog, Javascript
javascript

The dollar sign ($) and the underscore (_) characters are JavaScript (js) 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 js 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 js, 54 possible letters are available: any lowercase letter (a through z), any uppercase letter (A through Z), $ and _.

What is the ($) 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 js, 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);} 

What about (_) Identifier 

A convention has also developed regarding the use of _, which is frequently used to preface the 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 — js 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 js 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.

Tagged under: _, $, dollar sign, identifier, identifiers, javascript, js, underscore

About Dauris

What you can read next

droid learning ai behavior
Machine Learning w/ eCommerce
Guide Bitcoins
How To Buy Bitcoin Anywhere?
website design
Different Type of Websites

Leave a Reply Cancel reply

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

Categories

Recent Posts

  • Did someone say Navigation to the rescue: Android Jetpack

    If you been around the block a few times with A...
  • Using Enum w/Swift

    understand enum is nothing fancy and all langua...
  • Shimmering in Android

    You may or may not  have been aware of certain ...
  • Android, SQLite & Kotlin

    The name of the database in Android is SQLite. ...
  • When over Switch in Kotlin

    Good afternoon Programming Community, I am glad...

© 2017. All rights reserved. Designed by Dauris Little

TOP
This site uses tracking cookies to personalize content and ads. AcceptLearn More