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

A `Map` in TypeScript is a built-in, generic iterable collection that stores key-value pairs. Unlike standard JavaScript objects, a `Map` permits keys of any data type—including objects, functions, and primitives—and strictly maintains the insertion order of its elements. TypeScript enhances the native JavaScript `Map` by enforcing strict type safety through generics (`Map<K, V>`).

## Instantiation and Typing

TypeScript utilizes generics to define the types for the keys (`K`) and values (`V`). If initialized with values, TypeScript can infer these types, but explicit declaration is standard practice for empty maps.

```typescript theme={"dark"}
// Instantiating an empty Map with explicit types
const nodeCache = new Map<string, object>();

// Instantiating with an iterable of tuples
const statusCodes = new Map<number, string>([
    [200, "OK"],
    [404, "Not Found"]
]);
```

## Core API

The `Map` object provides a standardized set of methods for mutation and retrieval. TypeScript ensures that arguments passed to these methods adhere to the defined `<K, V>` generics.

* **`set(key: K, value: V): this`**

  Adds or updates an element with a specified key and value. Returns the `Map` instance, allowing for method chaining.

```typescript theme={"dark"}
statusCodes.set(500, "Internal Server Error").set(403, "Forbidden");
```

* **`get(key: K): V | undefined`**

  Returns the value associated with the key. If the key does not exist, it returns `undefined`. TypeScript automatically types the return value as a union of the value type and `undefined`.

```typescript theme={"dark"}
const status = statusCodes.get(200); // Type: string | undefined
```

* **`has(key: K): boolean`**

  Returns a boolean asserting whether a value has been associated with the key in the `Map`.

```typescript theme={"dark"}
const exists = statusCodes.has(404); // true
```

* **`delete(key: K): boolean`**

  Removes the specified element from the `Map`. Returns `true` if the element existed and was removed, or `false` if the element did not exist.

```typescript theme={"dark"}
statusCodes.delete(403);
```

* **`clear(): void`**

  Removes all key-value pairs from the `Map`, resetting its size to zero.

```typescript theme={"dark"}
statusCodes.clear();
```

* **`size: number`**

  A read-only property that returns the total number of elements currently in the `Map`.

```typescript theme={"dark"}
const count = statusCodes.size;
```

## Iteration Protocols

`Map` implements the iterable protocol, meaning it can be used directly with `for...of` loops. It yields key-value pairs as tuples (`[K, V]`).

```typescript theme={"dark"}
for (const [code, message] of statusCodes) {
    console.log(code, message);
}
```

Additionally, `Map` provides methods that return `IterableIterator` objects for specific parts of the collection:

* **`keys(): IterableIterator<K>`**

  Returns a new iterator object containing the keys for each element in insertion order.
* **`values(): IterableIterator<V>`**

  Returns a new iterator object containing the values for each element in insertion order.
* **`entries(): IterableIterator<[K, V]>`**

  Returns a new iterator object containing an array of `[key, value]` for each element in insertion order. This is the default iterator behavior of the `Map` object.

## Key Equality

Key equality in a `Map` is evaluated using the `SameValueZero` algorithm. This means `NaN` is considered equal to `NaN` (even though `NaN !== NaN` in standard JavaScript), and all other values are considered equal according to the semantics of the strict equality (`===`) operator.

When using objects or arrays as keys, equality is determined by memory reference, not structural shape.

```typescript theme={"dark"}
const map = new Map<object, string>();
const objKey = { id: 1 };

map.set(objKey, "Active");

// Successful retrieval: exact same reference
map.get(objKey); // Returns "Active"

// Failed retrieval: structurally identical, but different reference
map.get({ id: 1 }); // Returns undefined
```

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