Node.js buffers are objects that store arbitrary binary data. Buffers have a length
property that contains the number of bytes in the buffer.
const buf = Buffer.from('Hello, World', 'utf8');
buf.length; // 12, same as 'Hello, World'.length
For buffers containing UTF8 encoded strings, the buffer’s length is equivalent to the length of the string. For example, if you read a text file from the file system using fs
, the resulting buffer’s length is the same as the number of characters in the text file.
const fs = require('fs');
fs.writeFileSync('./test.txt', 'Hello, World');
const buf = fs.readFileSync('./test.txt');
Buffer.isBuffer(buf); // true
buf.length; // 12, same as 'Hello, World'.length
#node #javascript #developer