SyntaxStudy
Sign Up

Buffer

class

The Buffer class handles binary data. Used for file I/O, streams, and network operations.

Syntax

Buffer.from(data) Buffer.alloc(size)

Example

javascript
// Create buffer from string
const buf = Buffer.from('Hello World', 'utf8');
console.log(buf.toString('hex'));    // hex string
console.log(buf.toString('base64')); // base64 string
console.log(buf.toString('utf8'));   // 'Hello World'

// Allocate empty buffer
const empty = Buffer.alloc(10);

// Convert base64
const decoded = Buffer.from('SGVsbG8=', 'base64').toString();