I've gotten this idea in my head that i shouldn't use a libraries variable, if the variable name begins with '_', as it is standard to write private variables that way.

But now i'm thinking i'm taking that too literally. If my choices are to use "_var" that points to a HTML element or add a unique attribute to said element, so that i can query the DOM to find it, should i just use the variable?

Do other people avoid private variables and if so, would you say this is too much effort?

  • A habit is a recurrent, often unconscious pattern of behavior that is acquired through frequent repetition -- so in order to be a bad habit, you have to do it with frequent repetition. So, if you find yourself using other's private variables as matter of course and without conscious thought, then perhaps that is indeed a bad habit.
    – Erik Eidt
    Jul 25 at 16:05
  • 1
    The wording "bad habit" expresses a wrong assumption, that this might only be a cosmetic issue. I think a better wording would be to ask if this will introduce a serious drawback.
    – Doc Brown
    Jul 26 at 8:15

1 Answer 1


When I stick an _ at the start of a variable name I'm saying this is not part of the public API. I reserve the right to change this later.

If you're ok with me breaking your stuff with an update then fine. Go ahead. If you want stability then make sure the public API doesn't support what you're trying to do before you sneak in the back door.

And, as Thomas Junk points out, if the API doesn't support your needs, feel free to make some noise about it. You might be doing something I simply didn't think of.

  • 2
    Additionally - in case functionality is missing: Write a pull-request to enhance said API and let others profit too from the result. Jul 25 at 15:34
  • 3
    In case you didn't know: ECMAScript has "real" private members as of the ECMAScript 2022 release 6 weeks ago, and they have been supported in every browser and runtime except IE for significantly longer than that. These are private members deserving of their name whose encapsulation cannot be broken. In fact, unlike Java, I believe they are even safe from reflection. Jul 25 at 17:06
  • @JörgWMittag I'm not 100% convinced being 'safe from reflection' isn't a bug. The point is to show people where the front door is. Not to lock every window. Which is probably why people still use _. Jul 25 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.