What Is the Purpose of the Dollar Sign in JavaScript

What is the purpose of the dollar sign in JavaScript?

A '$' in a variable means nothing special to the interpreter, much like an underscore.

From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.

The dollar sign function $() in jQuery is a library function that is frequently used, so a short name is desirable.

Why would a JavaScript variable start with a dollar sign?

Very common use in jQuery is to distinguish jQuery objects stored in variables from other variables.

For example, I would define:

var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itself

I find this to be very helpful in writing jQuery code and makes it easy to see jQuery objects which have a different set of properties.

What is the meaning of "$" sign in JavaScript

Your snippet of code looks like it's referencing methods from one of the popular JavaScript libraries (jQuery, ProtoType, mooTools, and so on).

There's nothing mysterious about the use of $ in JavaScript. $ is simply a valid JavaScript identifier.

JavaScript allows upper and lower letters, numbers, and $ and _. The $ was intended to be used for machine-generated variables (such as $0001).

Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it. In that case you use jQuery instead of $. In fact, $ is just a shortcut for jQuery.

What does ${} (dollar sign and curly braces) mean in a string in JavaScript?

You're talking about template literals.

They allow for both multiline strings and string interpolation.

Multiline strings:

console.log(`foobar`);// foo// bar

Reading a javascript script, why so many $ (dollar signs)?

I think you are reading a JavaScript library famously known as jQuery (or possibly another library). The $ is just a short form for a namespace or use as an identifier.

You can think of it like this jQuery('p') to select all the paragraphs on a page,
or for short form you can just write $('p').

Here is a link for jQuery tutorials/docs jQuery

Here is a list of standards section 7.6 describes it in detail ECMA Standard

Why use $ (dollar sign) in the name of javascript variables?

The $ in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.

var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');

Now later in your code, you know the $myHeaderDiv is already a jQuery object, so you can call jQuery functions:

$myHeaderDiv.fade();

To get from the DOM-variable to the jQuery variable:

var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly

//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use

To get from the jQuery variable to the DOM-variable.

var myHeaderDiv = $myHeaderDiv.get(0);

What does the dollar prefix ($) mean in Vue.js?

The use of the $ and _ prefixes in Vue are explained here:

https://v2.vuejs.org/v2/style-guide/#Private-property-names-essential

Specifically in the Detailed Explanation section.

_ is for private instance properties:

Vue uses the _ prefix to define its own private properties...

$ is for public instance properties:

As for the $ prefix, its purpose within the Vue ecosystem is special instance properties that are exposed to the user...

Both are used to avoid collisions with property names chosen by component creators, such as props and data properties.


The $ prefix is not only used by Vue's core APIs. It's also commonly used by libraries that add properties to components. e.g.:

  • Vuex adds $store.
  • Vue Router adds $route and $router.

These are both officially supported libraries but the same is true of many third-party libraries.

It can also be used by application code that creates global properties. A common example is adding $http to Vue.prototype (or globalProperties in Vue 3).

In all of these cases, the $ acts as an indicator to future developers that a property is defined elsewhere and not within the current component.

What is the dollar sign in Javascript, if not jQuery

1) Is it correct that dollar sign was not assigned, a few years back, or do I remember wrong?

That's correct and still true.

2) What is the dollar sign, if not jQuery?

Firefox and Chrome implement $, $$ and several others as helper commands. Both will set $$ to document.querySelectorAll(), and set $ to document.querySelector if window.$ hasn't been defined.

So what you're seeing isn't actually standard JavaScript, but a helper in the developer console of your browser. It's also not jQuery (as long as you're not on a page using jQuery). However, it's behaviour is close to the one of jQuery, concerning that querySelector (for single matches) and querySelectorAll (for multiple matches) give you almost the same strength as the jQuery selector.

Why do I see a dollar sign before some constructors in JavaScript?

One website told me this:

Rather than having to type out jQuery
each time, we use an alias, a simple
dollar sign (the shortest legal
identifier that’s not alphanumeric).



Related Topics



Leave a reply



Submit