0

I came across the scenario in Angular 11 where the variable is declared with $ symbol.

e.g. $testVar = new Subject<Type[]>(this.name);

What is the meaning of this declaration with $ symbol?

Yogesh Nikam
  • 443
  • 1
  • 4
  • 13
  • 2
    The dollar symbol is more often used as a suffix, not a prefix. Maybe this answer will help : https://stackoverflow.com/a/37928549/6444705 – Emilien Aug 19 '21 at 14:01
  • 1
    Does this answer your question? [angular2 style guide - property with dollar sign?](https://stackoverflow.com/questions/37671700/angular2-style-guide-property-with-dollar-sign) – Austin T French Aug 19 '21 at 14:15
  • @AustinTFrench - I understand when $ added as a suffix to indicate an observable but in one case I observed it as prefix. So just curious to know if it has any significance. Thanks – Yogesh Nikam Aug 20 '21 at 06:42
  • `$` prefix meant a DOM node in jQuery. BTW, `#` prefix marks a variable as a JavaScript Private Field. – Ben Racicot Dec 14 '22 at 20:39

1 Answers1

2

It does not have any special function in TypeScript but there is a convention of using $ symbol in the name of the variable which stores an Observable. As Subject would create an Observable it might be used for this purpose here.

However the more common version of this is to use $ as suffix like this testVar$ - seeing that I would expect an Observable to be stored there.

But I would not use this suffix for variable storing the subject but rather the final Observable.


testSubject = new Subject<Type[]>(this.name);
test$ = testSubject.asObservable();

Lukasz Gawrys
  • 1,234
  • 2
  • 8
  • Thanks Luksz for the clarification. I used $ as a suffix for the observable but was curious if it has any significance if it used as a prefix. Thanks – Yogesh Nikam Aug 20 '21 at 06:44