# Get the length of a string in bytes

Count UTF-8 bytes rather than characters.

Canonical: https://1loc.dev/string/get-the-length-of-a-string-in-bytes/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function byteLength(text) { return new TextEncoder().encode(text).length; }
```

## Input

```json
"héllo"
```

## Expected output

```json
6
```

## Edge case

Input:
```json
"😀"
```
Output:
```json
4
```

## Limits

- UTF-8 is what most APIs and email bodies use. Email header lines are limited to 998 bytes excluding CRLF (RFC 5322); measure bytes, not characters.
- The original used new Blob([text]).size, which also measures UTF-8.

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/get-the-length-of-a-string-in-bytes.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 TextEncoder instead of Blob so it runs in workers and Node.js alike. Original MDX retained as inert source evidence.
