# Convert decimal to binary recursively

Build the binary digits of a non-negative integer as a decimal-looking number.

Canonical: https://1loc.dev/number/convert-decimal-to-binary-recursively/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function decToBinary(num) { return num === 0 ? 0 : (num % 2) + 10 * decToBinary(Math.floor(num / 2)); }
```

## Input

```json
10
```

## Expected output

```json
1010
```

## Edge case

Input:
```json
0
```
Output:
```json
0
```

## Limits

- The result is a number whose digits are bits, so the lab stops at 65,535 to stay exact. For real work use num.toString(2).
- The original ~~ truncation is replaced with Math.floor.

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/convert-decimal-to-binary-recursively.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: philosophercode
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses Math.floor instead of ~~ and caps input so the digit number stays exact. Original MDX retained as inert source evidence.
