# Calculate the distance between two points

Apply the Pythagorean theorem with Math.hypot.

Canonical: https://1loc.dev/math/calculate-the-distance-between-two-points/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function distance({ p1, p2 }) { return Math.hypot(p2.x - p1.x, p2.y - p1.y); }
```

## Input

```json
{
  "p1": {
    "x": 0,
    "y": 0
  },
  "p2": {
    "x": 3,
    "y": 4
  }
}
```

## Expected output

```json
5
```

## Edge case

Input:
```json
{
  "p1": {
    "x": 1,
    "y": 1
  },
  "p2": {
    "x": 1,
    "y": 1
  }
}
```
Output:
```json
0
```

## Limits

- Math.hypot avoids overflow in intermediate squares that Math.sqrt(dx² + dy²) can hit.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/calculate-the-distance-between-two-points.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses Math.hypot instead of Math.sqrt of squared differences. Original MDX retained as inert source evidence.
