> ## 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 Class Expression

A class expression is a syntax for defining a class in JavaScript where the class is evaluated as an expression rather than a statement. Like function expressions, class expressions can be named or anonymous and are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments, or returned from functions.

## Syntax

Class expressions utilize the `class` keyword followed by an optional binding identifier, an optional `extends` clause for inheritance, and the class body.

```javascript theme={"dark"}
class [className] [extends BaseClass] {
  // class body
}
```

**Anonymous Class Expression:**

```javascript theme={"dark"}
const Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
```

**Named Class Expression with `extends`:**

```javascript theme={"dark"}
const Square = class SquareClass extends Rectangle {
  constructor(side) {
    super(side, side);
  }
  
  getClassName() {
    // The internal name is accessible within the class body
    return SquareClass.name; 
  }
};
```

## Technical Characteristics

**Binding and Scope**
In a named class expression, the identifier following the `class` keyword (e.g., `SquareClass`) is local to the class body's scope. It cannot be referenced from the enclosing lexical scope. The external variable (e.g., `Square`) holds the actual reference to the class object.

Crucially, this internal binding is immutable. Because class bodies execute in strict mode, attempting to reassign the internal name within the class body will throw a `TypeError`.

```javascript theme={"dark"}
const MyClass = class InnerClass {
  constructor() {
    InnerClass = {}; // Throws TypeError: Assignment to constant variable.
  }
};
```

**The `name` Property**
The `name` property of a class expression is determined by its binding identifier. If the expression is anonymous, the JavaScript engine infers the `name` property from the variable it is assigned to. If it is named, the `name` property strictly matches the internal identifier.

```javascript theme={"dark"}
const AnonymousClass = class {};
console.log(AnonymousClass.name); // Output: "AnonymousClass"

const NamedClass = class InnerName {};
console.log(NamedClass.name); // Output: "InnerName"
```

**Hoisting and the Temporal Dead Zone (TDZ)**
Class expressions are not hoisted. The variable holding the class expression is subject to the hoisting rules of its declaration keyword (`let`, `const`, or `var`):

* **`let` and `const`:** The variable resides in the Temporal Dead Zone (TDZ) until the lexical binding is evaluated. Attempting to instantiate the class before the expression is evaluated will throw a `ReferenceError`.
* **`var`:** The variable is hoisted and initialized to `undefined`. Attempting to instantiate the class before the expression is evaluated will throw a `TypeError` (e.g., `AnonymousClass is not a constructor`) because the engine is attempting to invoke `undefined`.

**Execution Context**
The code within the body of a class expression is implicitly executed in strict mode (`'use strict'`). This applies to the constructor, static methods, and prototype methods, enforcing stricter parsing and error handling rules (such as prohibiting undeclared variables) regardless of the enclosing execution context.

<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>
