What is values() in Node.js Buffer Module?
The values function in Node.js Buffer Module
The values function is a member function of Buffer class in the Node.js Buffer Module. The values function is called on an instance of Buffer and returns an Iterator for the bytes in the instance. The syntax of the values function is as follows:
buf.values()
Parameters
values takes no parameters.
Return value
- Returns an
Iteratorfor values ofbuf.
Description
The values function creates and returns an Iterator for the values of the buffer buf.
Example usage of the values function
The following code snippet provides an example of how to use the values function:
const b = Buffer.from('ABCXYZ');for (const v of b.values()) {console.log(v);}
In the above example, b is a Buffer instance of length 6 that contains utf-8 encoded bytes for the string ABCXYZ. The b.values() function returns an Iterator, which is stored in v. In every iteration of the for loop, the value of v is printed, which is basically an integer value for a byte of buffer b.
Note: In a
for...ofconstruct,b.values()is unnecessary asbitself would also return a valueIteratorin this case.
Free Resources