Categories
JavaScript Answers

How to get the number of bytes in a JavaScript string?

Spread the love

Sometimes, we want to get the number of bytes in a JavaScript string.

In this article, we’ll look at how to get the number of bytes in a JavaScript string.

How to get the number of bytes in a JavaScript string?

To get the number of bytes in a JavaScript string, we can convert it to a blob.

For instance, we write

const size = new Blob(["I'm a string"]).size;

to create a blob from the "I'm a string" string with the Blob constructor.

Then we get the number of bytes in the string with the size property.

Conclusion

To get the number of bytes in a JavaScript string, we can convert it to a blob.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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