Open In App

Node.js Buffer.byteLength() Method

The Buffer.byteLength() method is used to return the length in bytes of the specified buffer object. 

Syntax:



Buffer.byteLength( string, encoding )

Parameters: This method accepts two parameters as mentioned above and described below:

Return Value: It returns the number of bytes of the specified object. 



Example 1: 




// Node.js program to demonstrate the
// Buffer.bytelength() method
 
// Create a buffer
const buf = Buffer.alloc(20);
 
// Check the length of a buffer object:
const lenobj = Buffer.byteLength(buf);
 
console.log(lenobj);

Output:

20

Example 2: 




// Node.js program to demonstrate the
// Buffer.bytelength() method
 
// Check the length of a buffer object:
const len = Buffer.byteLength('GeeksForGeeks');
 
console.log(len);

Output:

13

Note:

Reference: https://nodejs.org/docs/latest-v11.x/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding

Article Tags :