Node.js | Buffer.keys() Method

The Buffer.keys() method is used to return an iterator object, containing the key of every byte in a buffer object.

Syntax:

Buffer.keys()

Parameters: This method does not accept any parameters.

Return Value: It returns an iterator object having the keys of the buffer.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// Buffer.keys() method  
var buf = Buffer.from('Hello World');
   
// Read in the iterator object
// and return the key number
for (index of buf.keys()) {
      console.log(index);
}

chevron_right


Output:

0
1
2
3
4
5
6
7
8
9 

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// Buffer.keys() method
var buf1 = Buffer.from('abc');
var buf2 = Buffer.from('1');
   
// Read in the first iterator object
// and return the key number
for(index of buf1.keys()) {
    console.log(index);
}
   
// Read in the first iterator object
// and return the key number
for(index of buf2.keys()) {
    console.log(index)
}

chevron_right


Output:

0
1
2
0

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/buffer.html#buffer_buf_keys

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.