Skip to main content
Buffer.fill - node__buffer.d.ts - Node documentation
method Buffer.fill

Usage in Deno

```typescript import { type Buffer } from "node:node__buffer.d.ts"; ```
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: ```js 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: ``` `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: ```js 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: ``` If `value` contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown: ```js import { Buffer } from 'node:buffer'; const buf = Buffer.allocUnsafe(5); console.log(buf.fill('a')); // Prints: console.log(buf.fill('aazz', 'hex')); // Prints: 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`.