# Linear scale of a number between two ranges

The arrow-function form of the same mapping.

Canonical: https://1loc.dev/math/linear-scale-of-a-number-between-two-ranges/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function scale({ value, from, to }) { return ((value - from[0]) * (to[1] - to[0])) / (from[1] - from[0]) + to[0]; }
```

## Input

```json
{
  "value": 0.5,
  "from": [
    0,
    1
  ],
  "to": [
    0,
    255
  ]
}
```

## Expected output

```json
127.5
```

## Edge case

Input:
```json
{
  "value": 2,
  "from": [
    0,
    1
  ],
  "to": [
    0,
    10
  ]
}
```
Output:
```json
20
```

## Limits

- Values outside the input range extrapolate, as the edge case shows.

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/linear-scale-of-a-number-between-two-ranges.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: iamandrewluca
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Takes value, from and to ranges as one object. Original MDX retained as inert source evidence.
