> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript Private Field

Private fields are class properties prefixed with a hash (`#`) that enforce strict encapsulation at the language level. Unlike the traditional underscore convention (`_property`), private fields provide "hard privacy"—they are completely inaccessible and invisible outside the lexical scope of the class body in which they are defined.

## Syntax and Declaration

Private fields must be explicitly declared in the class body before they can be accessed or mutated. They cannot be dynamically added to an instance at runtime.

```javascript theme={"dark"}
class Container {
  // Mandatory upfront declaration
  #internalState;
  #initializedState = 100;

  constructor(value) {
    // Access via 'this.#fieldName'
    this.#internalState = value;
  }

  readState() {
    return this.#internalState + this.#initializedState;
  }
}

const instance = new Container(50);
console.log(instance.readState()); // 150
```

## Core Mechanics and Rules

**1. Strict Lexical Scoping**
Accessing a private field outside of its defining class results in a `SyntaxError`. This is evaluated at compile-time, preventing execution entirely.

```javascript theme={"dark"}
const instance = new Container(50);
console.log(instance.#internalState); // SyntaxError: Private field '#internalState' must be declared in an enclosing class
```

**2. Bracket Notation Incompatibility**
Private fields cannot be accessed using bracket notation. Attempting to do so will interact with a public string property that happens to start with a hash, rather than the actual private field.

```javascript theme={"dark"}
class BracketTest {
  #secret = 'hidden';
  
  constructor() {
    this['#secret'] = 'public'; 
  }

  getSecret() {
    return this.#secret; // Returns 'hidden'
  }
}
```

**3. Inheritance Restrictions**
Private fields are bound strictly to the class that defines them. Subclasses cannot access the private fields of their parent classes.

```javascript theme={"dark"}
class Parent {
  #parentData = 'data';
}

class Child extends Parent {
  accessParent() {
    return this.#parentData; // SyntaxError
  }
}
```

## Static Private Fields and Methods

The `#` prefix can also be applied to static properties and class methods (both instance and static). Static private fields are evaluated on the class constructor itself rather than the instance.

```javascript theme={"dark"}
class Utility {
  static #cache = new Map();

  static #compute(key) {
    return key * 2;
  }

  static process(key) {
    if (!this.#cache.has(key)) {
      this.#cache.set(key, this.#compute(key));
    }
    return this.#cache.get(key);
  }
}
```

## Ergonomic Brand Checks

JavaScript provides a mechanism to safely check if an object possesses a specific private field using the `in` operator. This is known as a "brand check" and will not throw an error if the field is missing, returning a boolean instead.

```javascript theme={"dark"}
class Node {
  #value;

  constructor(value) {
    this.#value = value;
  }

  static isNode(obj) {
    // Evaluates to true if 'obj' has the '#value' private field
    return #value in obj;
  }
}

const validNode = new Node(1);
const fakeNode = { value: 1 };

console.log(Node.isNode(validNode)); // true
console.log(Node.isNode(fakeNode));  // false
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
