Skip to main content
BufferConstructor.byteLength - node__buffer.d.ts - Node documentation
method BufferConstructor.byteLength

Usage in Deno

```typescript import { type BufferConstructor } from "node:node__buffer.d.ts"; ```
BufferConstructor.byteLength(
string:
string
| Buffer
| ArrayBufferView
| ArrayBuffer
| SharedArrayBuffer
,
encoding?: BufferEncoding,
): number
Returns the byte length of a string when encoded using `encoding`. This is not the same as [`String.prototype.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length), which does not account for the encoding that is used to convert the string into bytes. For `'base64'`, `'base64url'`, and `'hex'`, this function assumes valid input. For strings that contain non-base64/hex-encoded data (e.g. whitespace), the return value might be greater than the length of a `Buffer` created from the string. ```js import { Buffer } from 'node:buffer'; const str = '\u00bd + \u00bc = \u00be'; console.log(`${str}: ${str.length} characters, ` + `${Buffer.byteLength(str, 'utf8')} bytes`); // Prints: ½ + ¼ = ¾: 9 characters, 12 bytes ``` When `string` is a `Buffer`/[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)/[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/- Reference/Global_Objects/TypedArray)/[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)/[`SharedArrayBuffer`](https://develop- er.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer), the byte length as reported by `.byteLength`is returned.

Parameters

string:
string
| Buffer
| ArrayBufferView
| ArrayBuffer
| SharedArrayBuffer
A value to calculate the length of.
optional
encoding: BufferEncoding = 'utf8'
If `string` is a string, this is its encoding.

Return Type

number
The number of bytes contained within `string`.