Skip to content

Get length of a string in bytes using JavaScript

To get the length of a string in bytes, first convert the string to Blob and use the size property of blob object to get its length in bytes.

Advertisements
const lengthInBytes = txt => new Blob([txt]).size;

console.log(lengthInBytes("Poopcode"))

//8
See also  Python code snippet - How to check if element is in list ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.