Categories
JavaScript Answers

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

Spread the love

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.

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 *