Skip to content
Related Articles

Related Articles

Node.js Buffer.length Property
  • Last Updated : 07 Aug, 2020

The Buffer.length property is an inbuilt application programming interface of class Buffer within buffer module which is used to get the length of this buffer object.

Syntax:

const Buffer.length

Parameters: This property does not accept any parameter.

Return Value: This property returns the length of this buffer object.

Example 1: Filename: index.js



Javascript




// Node.js program to demonstrate the
// Buffer.length property
  
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
  
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
  
// Getting byteoffset of buffer
// by using byoffset api
const length = buffer.length;
  
// Display the result
console.log("Length is : " + length);

Output:

Length is : 16

Example 2: Filename: index.js

Javascript




// Node.js program to demonstrate the
// Buffer.length property
  
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
  
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
  
// Getting byteoffset of buffer
// by using byoffset api
const length = buffer.length;
  
// Creating and initializing Int8Array object
const buff = new Int8Array(buffer, buffer.byteoffset, length);
  
// Display the result
console.log("Int8Arry object :- " + buff);

Output:

Int8Arry object :- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

Run the index.js file using the following command:

node index.js

Reference:https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buf_length

My Personal Notes arrow_drop_up
Recommended Articles
Page :