Node.js Buffer.keys() Method
Last Updated :
11 Apr, 2023
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: In this example, we will use the Buffer.keys() method
JavaScript
const buf = Buffer.from( 'Hello World' );
for (index of buf.keys()) {
console.log(index);
}
|
Output:
0
1
2
3
4
5
6
7
8
9
Example 2: In this example, we will use the Buffer.keys() method
Javascript
const buf1 = Buffer.from( 'abc' );
const buf2 = Buffer.from( '1' );
for (index of buf1.keys()) {
console.log(index);
}
for (index of buf2.keys()) {
console.log(index)
}
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...