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

# TypeScript Exponentiation

The `**` operator is the exponentiation operator in TypeScript, evaluating to the result of raising the left operand (base) to the power of the right operand (exponent). While mathematically equivalent to `Math.pow()`, the `**` operator natively supports both `number` and `bigint` operands. In contrast, `Math.pow()` strictly accepts `number` types; passing a `bigint` to `Math.pow()` results in a TypeScript compile-time error (`Argument of type 'bigint' is not assignable to parameter of type 'number'`).

```typescript theme={"dark"}
base ** exponent
```

## Type Constraints and BigInt Behavior

TypeScript enforces strict type checking on the operands. Both the base and the exponent must be of type `number` or `bigint`, and both operands must be of the exact same type. Mixing types will result in a compiler error.

Furthermore, when using `bigint` operands, the exponent must be non-negative. Because the `bigint` type cannot represent fractional values, evaluating a `bigint` base with a negative `bigint` exponent throws a runtime `RangeError`.

```typescript theme={"dark"}
const num: number = 3 ** 4;       // Valid: 81
const big: bigint = 2n ** 64n;    // Valid: 18446744073709551616n

// @ts-expect-error: Operator '**' cannot be applied to types 'bigint' and 'number'.
const mixed = 2n ** 3;            

// Compiles successfully, but throws a runtime RangeError: BigInt negative exponent
const negativeBigIntExponent = 2n ** -1n;
```

## Associativity

Unlike most binary operators in TypeScript (which are left-associative), the `**` operator is **right-associative**. Multiple exponentiation operations in a single expression are evaluated from right to left.

```typescript theme={"dark"}
const result = 2 ** 3 ** 2;

// Evaluates as: 2 ** (3 ** 2) -> 2 ** 9 -> 512
// NOT as: (2 ** 3) ** 2 -> 8 ** 2 -> 64
```

## Precedence and Unary Operators

The `**` operator has a higher precedence than standard arithmetic operators (`*`, `/`, `+`, `-`). However, TypeScript enforces a strict parsing rule: a unary operator (such as `-`, `+`, `~`, `!`) cannot immediately precede the base operand of an unparenthesized exponentiation expression. This prevents ambiguity regarding whether the unary operator applies to the base or the evaluated result.

```typescript theme={"dark"}
// TS SyntaxError: Unary operator used immediately before exponentiation expression.
// const invalid = -2 ** 2; 

// Valid: Unary operator applied to the base
const negativeBase = (-2) ** 2;   // Evaluates to 4

// Valid: Unary operator applied to the evaluated exponentiation expression
const negativeResult = -(2 ** 2); // Evaluates to -4
```

## Compound Assignment

The operator includes a compound assignment variant, `**=`, which applies the exponentiation operation to a variable and assigns the result back to that variable.

```typescript theme={"dark"}
let x: number = 5;
x **= 3; // Syntactic sugar for: x = x ** 3
// x is now 125
```

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