# Converting Minecraft Coordinates in JavaScript: Overworld, Nether and Chunks

Minecraft coordinates look simple, but a few details trip people up: floor division with negative numbers, the 8:1 Nether ratio, and chunk/region math. Here is a small, tested set of JavaScript helpers.

## Floor division done right

JavaScript's `Math.trunc` and the `|0` trick round toward zero, which is wrong for negative coordinates. Minecraft uses floor division.

```javascript
const floorDiv = (a, b) => Math.floor(a / b);

floorDiv(-1, 16);  // -1 (correct)
(-1 / 16) | 0;     //  0 (wrong)
```

## Overworld and Nether

```javascript
function toNether({ x, y, z }) {
  return { x: floorDiv(x, 8), y, z: floorDiv(z, 8) };
}

function toOverworld({ x, y, z }) {
  return { x: x * 8, y, z: z * 8 };
}

toNether({ x: 2400, y: 70, z: -1601 }); // { x: 300, y: 70, z: -201 }
```

Y is not scaled. Portals search a 16-block radius in the Nether (128 in the Overworld), which is why portals built too close together link unpredictably.

## Block to chunk and region

```javascript
const blockToChunk = (x, z) => ({ cx: x >> 4, cz: z >> 4 });
const chunkToRegion = (cx, cz) => ({ rx: cx >> 5, rz: cz >> 5 });

blockToChunk(-1, 17);     // { cx: -1, cz: 1 }
chunkToRegion(-1, 1);     // { rx: -1, rz: 0 } -> file r.-1.0.mca
```

Bit shifts are arithmetic for 32-bit ints in JS, so `>> 4` is a correct floor division by 16 for negative numbers within the world border.

## Chunk bounds

```javascript
function chunkBounds(cx, cz) {
  return {
    minX: cx * 16, maxX: cx * 16 + 15,
    minZ: cz * 16, maxZ: cz * 16 + 15,
  };
}
```

Handy for slime chunk checks, farm placement and spawn-chunk planning.

## Quick tests

```javascript
console.assert(toNether({ x: -9, y: 0, z: 0 }).x === -2);
console.assert(blockToChunk(15, 16).cz === 1);
console.assert(chunkToRegion(31, 32).rz === 1);
```

## When you do not want to code it

For quick lookups while playing, a [Minecraft coordinate converter](https://minecraftseedmap.org/tools/coordinate-converter) handles Overworld/Nether and chunk conversions without opening a console.

These few helpers cover most map tools, portal planners and world-editing scripts you will write.
