Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/node/_buffer.d.ts>Buffer#fill

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
method Buffer.prototype.fill
import { Buffer } from "https://deno.land/std@0.145.0/node/_buffer.d.ts";

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

import { Buffer } from 'buffer';

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

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

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

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 & 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 '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 '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.

optional
offset: number
optional
end: number
optional
encoding: Encoding

Returns

this

A reference to buf.