Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript private class fields use a #-prefixed name, such as #count, to keep class state accessible only from code that declares that private name. They are established syntax, not a newly arriving feature: MDN reports broad browser availability since July 2021. This guide shows how to declare and use private instance and static fields, what the privacy boundary does—and does not—mean, and how to check whether an object carries a private field.

How to use private class fields in JavaScript?

Declare a private field in the class body, then access it from that class’s methods or other class code where the name is in scope. For example:

class Counter {
  #count = 0;

  increment() {
    this.#count++;
  }

  get value() {
    return this.#count;
  }
}

const counter = new Counter();
counter.increment();
console.log(counter.value); // 1

#count = 0; declares and initializes an instance field. Each Counter instance gets its own field as the instance is initialized. The field is read and updated using this.#count from code within Counter. The public getter provides a controlled way to expose a value without exposing the private field itself.

A private name must be declared in the class body before class code can use it. A direct expression such as counter.#count outside the declaring class is a syntax error. Do not use bracket notation: counter["#count"] refers to an ordinary string key, not the private field.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

What does # mean in a JavaScript class?

A hash-prefixed identifier is a private name recognized by JavaScript’s class syntax. It is not a normal object property whose name happens to begin with a hash. The language enforces access at runtime and at parse time: code that does not have the private name in scope cannot read or write it through ordinary property access.

Private elements do not act like enumerable keys. Ordinary object utilities cannot enumerate, delete, or otherwise interact with them as if they were public properties; they also cannot be proxied as private elements. Operations such as Object.freeze() and Object.seal() do not freeze private elements, and structuredClone() does not copy them. For these details and the syntax rules, see MDN’s Private elements reference.

This is language-enforced encapsulation, not a promise that the containing object, program, or runtime is secure in every other respect. Use a public method or accessor when callers need supported access to a value, and treat that API—not the private field—as the deliberate boundary.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Can a subclass access a private field?

No. A private name belongs lexically to the class that declares it. A subclass cannot refer to a base class’s private field, even though instances of the subclass also pass through the base class constructor. A subclass may declare the same spelling, but that creates a separate private field:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
class Base {
  #value = "base";

  readBaseValue() {
    return this.#value;
  }
}

class Child extends Base {
  #value = "child";

  readChildValue() {
    return this.#value;
  }
}

const item = new Child();
console.log(item.readBaseValue());  // "base"
console.log(item.readChildValue()); // "child"

If a subclass needs behavior based on base-class state, expose that behavior through a base-class method or accessor rather than trying to access the base class’s private name directly. Private identifiers in one class also must be unique across its static and instance elements, except for a getter/setter pair; #constructor is invalid.

How do private static fields work?

A private static field belongs to the declaring class constructor rather than to each instance. Declare it with static, and use it from class code:

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
class Sequence {
  static #nextId = 1;

  static nextId() {
    return this.#nextId++;
  }
}

console.log(Sequence.nextId()); // 1
console.log(Sequence.nextId()); // 2

Static private access has an inheritance trap when a method uses this.#value. If that method is inherited and called on a subclass, this is the subclass constructor, which did not declare the base class’s private static field:

class Base {
  static #value = 1;

  static read() {
    return this.#value;
  }
}

class Child extends Base {}
Child.read(); // TypeError

If the intended owner is specifically the base class, the base class’s method can refer to Base.#value instead. That deliberately reads the declaring class’s field; it is not a mechanism for giving subclasses access to that private slot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I check whether an object has a private field?

Use the in operator with the private name from within code where that name is declared. It returns a boolean indicating whether the object carries that private element:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
class Counter {
  #count = 0;

  static hasCount(object) {
    return #count in object;
  }
}

console.log(Counter.hasCount(new Counter())); // true
console.log(Counter.hasCount({}));            // false

The check must appear in a scope that can refer to #count; outside that class, #count in object is not valid. Directly trying to access a private field on an object that does not carry it throws a TypeError, rather than returning undefined.

What changes when a Proxy wraps an instance?

A proxy is a different object from its target and does not automatically carry the target’s private elements. If a method that reads a private field is called with the proxy as its receiver, private access can fail. When a wrapper must preserve private-field access, arrange for the method to run with the original instance as its receiver—for example, by binding it to the target—while considering the API consequences of doing so. Do not assume a proxy or ordinary object-inspection APIs can expose or forward private elements like public keys.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do private fields compare with underscore names and WeakMap state?

Approach Access control How state is accessed
#field JavaScript-enforced private access within the declaring class’s scope. Class methods and other code in scope use syntax such as this.#field; ordinary property APIs do not treat it as a key.
_field Naming convention only; callers can still access the ordinary property. Ordinary property access, including from outside the class.
Closure or WeakMap Alternative encapsulation techniques whose access depends on where the closure or map is held. Requires separately structured closure or map logic rather than direct private-field syntax.

These approaches are not interchangeable security guarantees. A leading underscore communicates intent but does not restrict access. Closures and WeakMap can keep data out of ordinary object properties, but use different code organization. The sources cited here do not establish a performance comparison, so choose based on encapsulation needs and maintainability rather than an assumed speed advantage.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Compatibility and tooling notes

MDN’s compatibility introduction says private elements have been available across browsers since July 2021. That is a broad browser-availability statement, not a guarantee for every older runtime or embedded JavaScript engine. If supporting legacy or embedded environments, check the actual runtime and any build tooling your project targets.

Chrome DevTools documents a console-only relaxation that can make private elements appear inspectable from outside. That behavior is a debugging convenience, not valid application syntax or a change to JavaScript’s private-access rules. For the normative language definition, consult the ECMAScript specification; background on the feature is available in the TC39 class-fields proposal repository.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.