Buffer toString() Method in Node.js

Let’s start with an example. The example is to display the buffer object as a string.

var buf = Buffer.from('abc');
console.log(buf.toString());

toString() method returns the buffer object according to the specified encoding. Its syntax is the following.

buffer.toString(encoding, start, end);

Here the parameters encoding, start, end represent the following. Three of these parameters are optional. Encoding represents the type of encoding used in the method to return value for example utf8. Start represents where the start is by default it is 0. End represents where to end and by default it is at the very end or the default value is buffer.length.

The start and end parameters are used to decode only a particular subset of a buffer. A default replacement character like U+FFFD will replace if the byte sequence in the buffer data is not valid to the given encoding. The return value is the decoded string from buffer to string depending on the specified encoding.