Search code examples
javascriptweb-component

what does # mean in javascript


Came across something I cant find in docs. This #selectedIndex = 0; what does # do?

class WcTabPanel extends HTMLElement {
    static observedAttributes = ["selected-index", "direction"];
    #selectedIndex = 0;

appears in this example https://codepen.io/ndesmic/pen/mdELqbM


Solution

  • It means private field.

    Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself.

    You can find documentation here (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields).