While classes are available in JavaScript since ES2015, they do not offer private attributes and methods. These possibilities were ruled out at the time of the initial release, due to disagreements within the TC39 committee. These draft specifications are intended to provide these capabilities to JavaScript classes shortly.


Once these specifications are formally accepted, private attributes and methods can be defined using the pound sign ‘# “. The decision to use the pound sign rather than the more traditional underscore’ _ ‘was made to avoid” breaking changes “in existing libraries that indicate private attributes through this.


This might seem counterintuitive, yet despite the existence of libraries indicating attributes or methods as private with an underscore, developers continue to use them.


The problem can be demonstrated with an anecdote from React, a popular JavaScript framework, which had to use variable names powerful enough to deter developers from using them (although they are marked as private), such as’ _



Each of the 3 TC39 committee proposals addresses a different aspect of private attributes and methods in classes. However, it should be safe to treat them as a single proposition as they will likely be added to JavaScript at the same time. The proposals are:



It is currently not possible to directly define class attributes in JavaScript. Instead, developers define these attributes in the constructor.

class Counter {

constructor() {

this.xValue = 0;



This proposal adds the possibility of defining public and private attributes directly in the definition of the class.

class Counter {

xValue = 0;

#yValue = 0;

constructor() { }




This proposal addresses the addition of private methods and the use of private accessors.

class Counter {

get #x() { return #xValue; }

set #x(value) { }


#clicked() { }




This proposal addresses the use of static, public and private fields and methods.

class CustomDate {

static public() = {}

static #private() = {}


It is important to note that the heavy use of private attributes and methods can damage a codebase. Private methods cannot be tested on a unitary basis and can often be an indication of problems with the Single Responsibility Principle.

Private attributes and methods have not yet been officially integrated into JavaScript, but developers can use them with Babel now. A TypeScript implementation is currently in the works and will be available in a later release.


Please enter your comment!
Please enter your name here