Complete the code to create a buffer of size 10 bytes.
const buf = Buffer.[1](10);
The Buffer.alloc(size) method creates a buffer of the specified size filled with zeros.
Complete the code to convert a string to a buffer.
const buf = Buffer.[1]('hello');
The Buffer.from(string) method creates a buffer containing the bytes of the given string.
Fix the error in the code to correctly create a buffer from an array.
const buf = Buffer.[1]([1, 2, 3]);
To create a buffer from an array of bytes, use Buffer.from(array).
Fill both blanks to create a buffer from a string and specify UTF-8 encoding.
const buf = Buffer.[1]('hello', [2]);
Buffer.from(string, encoding) creates a buffer from a string with the specified encoding, UTF-8 is default and common.
Fill all three blanks to create a buffer from a string, specify encoding, and convert it back to string.
const buf = Buffer.[1]('hello', [2]); const str = buf.toString([3]);
This code creates a buffer from a string using UTF-8 encoding and then converts the buffer back to a string using the same encoding.