Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the hash character (#) used for in JavaScript?

Tags:

javascript

I'm not talking about in the URL. I know what that does. I'm talking about how it's used in actual code.

After trying to assign it as a variable, I realized that it's reserved, but I don't know what for.

like image 905
McKayla Avatar asked Aug 31 '25 23:08

McKayla


2 Answers

An ongoing proposal (currently in stage 4) utilizes hashtags to mark fields as private. It is part of the ES2022 standard.

Example:

class Foo {
  x = 1; // public field
  #y = 2; // private field

  add() {
    return this.x + this.#y;
  }
}

like image 99
thabs Avatar answered Sep 03 '25 14:09

thabs


Javascript, or more precisely ECMAscript, is an evolving language. Some symbols and keywords (such as "class") have been reserved for future versions, even though they may not have any meaning at the moment.

like image 41
Bart Avatar answered Sep 03 '25 12:09

Bart