The Buffer Class

A Buffer is a Uint8Array subclass with fixed-width numeric accessors, string decoding and a pooled allocator added. Create one with Buffer.from (copies a value), Buffer.alloc (zero-filled) or Buffer.allocUnsafe (uninitialized: faster, but it exposes old memory contents until you overwrite them).

Allocating, inspecting and slicing buffersJavaScript
const buf = Buffer.from('Node 26', 'utf8');
console.log(buf, buf.length, buf instanceof Uint8Array);
console.log(buf.toString('hex'), buf.toString('base64url'));
const header = Buffer.alloc(8);
header.writeUInt32BE(0xdeadbeef, 0); header.writeUInt16LE(513, 4);
console.log(header, header.readUInt32BE(0).toString(16), header.readUInt16LE(4));
const view = buf.subarray(0, 4); view[0] = 0x6e;   // a view, not a copy
console.log(buf.toString(), Buffer.compare(view, Buffer.from('node')));
Output
<Buffer 4e 6f 64 65 20 32 36> 7 true
4e6f6465203236 Tm9kZSAyNg
<Buffer de ad be ef 01 02 00 00> deadbeef 513
node 26 0

subarray returns a view over the same memory, so writing through view changed buf; Buffer.from(source) copies. The accessors name their byte order because binary formats do: writeUInt16LE(513) produced 01 02, the reverse of a big-endian write.

Compare buffers with buf.equals(other), or Buffer.compare for sorting, never ===. For secret material use crypto.timingSafeEqual (Cryptography), so comparison time does not leak the contents.