[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Get the length of a string in bytes

Get the length of a string in bytes

JavaScript

const bytes = (str) => new Blob([str]).size

TypeScript

const bytes = (str: string): number => new Blob([str]).size

Examples

bytes('hello world') // 11
bytes('🎉') // 4