How to convert string into UTF-8 with Node.js?

Sometimes, we want to convert string into UTF-8 with Node.js.

In this article, we’ll look at how to convert string into UTF-8 with Node.js.

How to convert string into UTF-8 with Node.js?

To convert string into UTF-8 with Node.js, we call the Buffer.from method.

For instance, we write

const someEncodedString = Buffer.from("foobar", "utf-8").toString();

to call Buffer.from with our buffer string and the string with the encoding of the buffer string.

Then we call toString to return the buffer converted to a string with the "utf-8" encoding.

Conclusion

To convert string into UTF-8 with Node.js, we call the Buffer.from method.