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.