Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js buf.fill() method

Reference for Node.js buf.fill(): fill a Buffer or a range with a byte, a repeating string or another buffer, with encodings, number wrapping and error cases.

buf.fill(value[, offset[, end]][, encoding]) writes a value into every byte of a range of the buffer, repeating the value as often as needed, and returns the same buffer so calls can be chained. The value can be a number (one byte), a string (its encoded bytes repeat) or a buffer or Uint8Array (its bytes repeat).

It changes the buffer in place. The most common use is clearing secrets or reusing a buffer, for example buf.fill(0). When you just need a new zeroed buffer, Buffer.alloc() is simpler.

NODE
const buf = Buffer.alloc(7);
buf.fill("ab");       // 61 62 61 62 61 62 61
buf.fill(0, 2, 4);    // zero only bytes 2 and 3

Try it

Node.js Repeating patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Numbers wrap, bad values throw

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
buf.fill(value, offset = 0, end = buf.length, encoding = "utf8")
buf.fill(value, encoding)
// returns Buffer (the same object)
Name Description
value A string, Buffer, Uint8Array or integer. Other values are converted to a number (for example true becomes 1).
offset Index where filling starts. Default 0.
end Index where filling stops, not included. Default buf.length.
encoding Encoding of value when it is a string. Default "utf8".

Returns: buf itself.

Errors and edge cases:

  • An offset or end outside 0 to buf.length throws ERR_OUT_OF_RANGE.
  • If offset is not below end, nothing is written.
  • A string or buffer that encodes to zero useful bytes throws ERR_INVALID_ARG_VALUE: an invalid hex string such as "zz", or an empty buffer. An empty string is the exception and fills with 0.
  • An unknown encoding throws ERR_UNKNOWN_ENCODING.
  • A multi-byte pattern that does not divide evenly is truncated at the end of the range, so a half character can remain.

Usage notes

  • Numbers only affect one byte. fill(0x1234) fills with 0x34. To repeat a wider value, write it into a small buffer first (see buf.writeUInt32LE()) and fill with that buffer.
  • Fast initialization: fill() is implemented natively, much faster than a JavaScript loop.
  • Do not trust allocUnsafe() memory. Fill it, or write every byte, before it can leave your code. See Buffer.allocUnsafe().
  • Wiping secrets with fill(0) removes your reference's contents, but the engine may have made other copies (strings, for example), so it is not a guarantee.
  • Buffer.alloc(size, fill) is shorthand for allocating and then calling fill().
  • Works on views: filling a subarray() changes the original memory.