Node.js Buffer.byteOffset Property

The Buffer.byteOffset property is an inbuilt application programming interface of class Buffer within buffer module which is used to get the byte offset value of this buffer.

Syntax:

const Buffer.byteOffset

Return Value: This property return the object of array buffer.

Example 1: Filename: index.js

Javascript



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// Buffer.byteOffset 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 property
const bytoff = buffer.byteOffset;
  
// Display the result
console.log("byteoffset is : " + bytoff);

chevron_right


Output:

byteoffset is : 0

Example 2: Filename: index.js

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// Buffer.byteOffset 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 bytoff = buffer.byteOffset;
  
const buff = new Int8Array(buffer, bytoff, buffer.length);
  
// Display the result
console.log("Int8Arry object : " + buff);

chevron_right


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_byteoffset

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.