ゆう's Blog
文字列のバイト数を調べる

UTF‑8 のバイト数を調べる

function byteLength(str) { return new TextEncoder().encode(str).length; } console.log(byteLength("あ")); // 3 console.log(byteLength("A")); // 1 console.log(byteLength("😊")); // 4