Example 1 — Example
Typical usage.
JavaScript
const enc = new TextEncoder();
const bytes = enc.encode("hi");
console.log(bytes.length);
console.log(bytes[0], bytes[1]);How It Works
- encode. UTF-8 bytes for "hi".
- 104/105. Code units for h/i.
TextEncoder turns JavaScript strings into UTF-8 bytes (Uint8Array).
const enc = new TextEncoder();
const bytes = enc.encode("hi");
console.log(bytes.length);
console.log(bytes[0], bytes[1]);Typical usage.
const enc = new TextEncoder();
const bytes = enc.encode("hi");
console.log(bytes.length);
console.log(bytes[0], bytes[1]);Check support.
console.log("TextEncoder — verify browser/Node support");CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful