Dollar Sign in Variable Name

When should I use the dollar symbol ($) in a variable name?

From the Java Language Specification on identifiers:

The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

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.

dollar sign in variable name?

The only legal characters according to the standard are alphanumerics
and the underscore. The standard does require that just about anything
Unicode considers alphabetic is acceptable (but only as single
code-point characters). In practice, implementations offer extensions
(i.e. some do accept a $) and restrictions (most don't accept all of the
required Unicode characters). If you want your code to be portable,
restrict symbols to the 26 unaccented letters, upper or lower case, the
ten digits, and the '_'.

Dollar sign in front of a variable name or no dollar sign

$ has no special meaning in a JavaScript identifier.

There is a convention to use a variable name starting with a $ character to represent a variable that will hold a jQuery object.

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.

What does $ sign at the end of function name indicate?

Syntactically, the dollar ($) character has no special meaning in JavaScript identifiers.

It is, however, sometimes used by convention to indicate that a variable holds an Observable or that a function will return an Observable.

What is the meaning of $ in a variable name?

$ is used internally by the compiler to decorate certain names. Wikipedia gives the following example:

public class foo {
class bar {
public int x;
}

public void zark () {
Object f = new Object () {
public String toString() {
return "hello";
}
};
}
}

Compiling this program will produce three .class files:

  • foo.class, containing the main (outer) class foo
  • foo$bar.class,
    containing the named inner class foo.bar
  • foo$1.class, containing the
    anonymous inner class (local to method foo.zark)

All of these class names are valid (as $ symbols are permitted in the JVM specification).

In a similar vein, javac uses $ in some automatically-generated variable names: for example, this$0 et al are used for the implicit this references from the inner classes to their outer classes.

Finally, the JLS recommends the following:

The $ character should be used only in mechanically generated source
code or, rarely, to access preexisting names on legacy systems.

What does a trailing dollar sign on a variable name mean in rxjs/Observables scope?

It is a convention followed by the Observable Angular docs explains it in details.

This convention was added after Angular 5 (so it might happen that it couldn't be found in lower versions before 5).

This is what Angular docs said about $ sign.

Naming conventions for observables

Because Angular applications are mostly written in TypeScript, you will typically know when a variable is an observable. Although the Angular framework does not enforce a naming convention for observables, you will often see observables named with a trailing “$” sign.

This can be useful when scanning through code and looking for observable values. Also, if you want a property to store the most recent value from an observable, it can be convenient to simply use the same name with or without the “$”.

angular2 style guide - property with dollar sign?

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable.
It could make it to the official style guide too but it's not there yet

Read more here : What does the suffixed dollar sign $ mean?

Update:
Read more about the trailing “$” sign on Angular website here:
https://angular.io/guide/rx-library#naming-conventions-for-observables



Related Topics



Leave a reply



Submit