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

Usage in Deno

```typescript import { type Buffer } from "node:node__buffer.d.ts"; ```
Buffer.write(
string: string,
encoding?: BufferEncoding,
): number
Writes `string` to `buf` at `offset` according to the character encoding in`encoding`. The `length` parameter is the number of bytes to write. If `buf` did not contain enough space to fit the entire string, only part of `string` will be written. However, partially encoded characters will not be written. ```js import { Buffer } from 'node:buffer'; const buf = Buffer.alloc(256); const len = buf.write('\u00bd + \u00bc = \u00be', 0); console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`); // Prints: 12 bytes: ½ + ¼ = ¾ const buffer = Buffer.alloc(10); const length = buffer.write('abcd', 8); console.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`); // Prints: 2 bytes : ab ```

Parameters

string: string
String to write to `buf`.
optional
encoding: BufferEncoding = 'utf8'
The character encoding of `string`.

Return Type

number
Number of bytes written.
Buffer.write(
string: string,
offset: number,
encoding?: BufferEncoding,
): number

Parameters

string: string
offset: number
optional
encoding: BufferEncoding

Return Type

number
Buffer.write(
string: string,
offset: number,
length: number,
encoding?: BufferEncoding,
): number

Parameters

string: string
offset: number
length: number
optional
encoding: BufferEncoding

Return Type

number