Node.js Buffer.write() Method
The Buffer.write() method writes the specified string into a buffer, at the specified position. If buffer did not contain enough space to fit the entire string, only part of string will be written. However, partially encoded characters will not be written.
buffer.write( string, offset, length, encoding )
Parameters: This method accept four parameters as mentioned above and described below:
- string: It holds the string to write into the buffer.
- offset: It holds the number of bytes to skip before starting to write the string. Its default value is 0.
- length: It holds the number of bytes to write into buffer. The default value is buffer.length-offset.
- encoding: It holds the encoding of the value. The default value is ‘utf8’.
Return Value: This method returns a number that represents the number of bytes written.
buf = Buffer.from(
const buf = Buffer.allocUnsafe(100);
const len = buf.write(
, 2, 5,
Note: The above program will compile and run by using the
node index.js command.