// Create a new buffer object with 4 bytes const buf = Buffer.alloc(4); // Fill buffer with value (in this case 0xff) buf.fill(255); // Output the buffer as a hexadecimal string console.log(buf.toString('hex')); // expected output: ffffffff
// Create a new buffer object from a string const buf = Buffer.from('Hello, World!'); // Output the buffer as a string console.log(buf.toString()); // expected output: Hello, World!This example creates a new buffer object using the `Buffer.from()` method and a string literal. The buffer is then output as a string using the `toString()` method. These examples use the built-in Buffer class in Node.js, which is a part of the Node.js core library. No additional package or library is required to use the Buffer class.