Utilities
TextEncoder.encode()
Encodes a string into UTF-8 bytes. Unicode character counts and encoded byte lengths differ; use byte counts when limiting binary payloads. This Web-compatible API is also exposed by Node. Tested on Node.js 22.16.0. Example output (one line per entry): ["5 caf\u00e9"].
Formal Syntax & Structure
nodejs syntaxnodejs
encoder.encode([input])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| input | string | Input string. |
Return Value
Uint8Array
UTF-8 bytes.
Working Example
Example Snippetnodejs
import { TextEncoder } from 'node:util';const bytes = new TextEncoder().encode('café');console.log(bytes.length, Buffer.from(bytes).toString('utf8'));