Buffer.fill - Node documentation
method Buffer.fill

Usage in Deno

import { type Buffer } from "node:buffer";
Buffer.fill(
value:
string
| Uint8Array
| number
,
offset?: number,
end?: number,
encoding?: BufferEncoding,
): this

Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled:

import { Buffer } from 'node:buffer';

// Fill a `Buffer` with the ASCII character 'h'.

const b = Buffer.allocUnsafe(50).fill('h');

console.log(b.toString());
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

// Fill a buffer with empty string
const c = Buffer.allocUnsafe(5).fill('');

console.log(c.fill(''));
// Prints: <Buffer 00 00 00 00 00>

value is coerced to a uint32 value if it is not a string, Buffer, or integer. If the resulting integer is greater than 255 (decimal), buf will be filled with value &#x26; 255.

If the final write of a fill() operation falls on a multi-byte character, then only the bytes of that character that fit into buf are written:

import { Buffer } from 'node:buffer';

// Fill a `Buffer` with character that takes up two bytes in UTF-8.

console.log(Buffer.allocUnsafe(5).fill('\u0222'));
// Prints: <Buffer c8 a2 c8 a2 c8>

If value contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown:

import { Buffer } from 'node:buffer';

const buf = Buffer.allocUnsafe(5);

console.log(buf.fill('a'));
// Prints: <Buffer 61 61 61 61 61>
console.log(buf.fill('aazz', 'hex'));
// Prints: <Buffer aa aa aa aa aa>
console.log(buf.fill('zz', 'hex'));
// Throws an exception.

Parameters

value:
string
| Uint8Array
| number

The value with which to fill buf. Empty value (string, Uint8Array, Buffer) is coerced to 0.

optional
offset: number = 0

Number of bytes to skip before starting to fill buf.

optional
end: number = buf.length

Where to stop filling buf (not inclusive).

optional
encoding: BufferEncoding = 'utf8'

The encoding for value if value is a string.

Return Type

this

A reference to buf.