DEV Community

Cover image for How to get the length of the Buffer in Node.js?
MELVIN GEORGE
MELVIN GEORGE

Posted on Originally published at melvingeorge.me

How to get the length of the Buffer in Node.js?

Originally posted here!

To get the length of a Buffer instance in Node.js, you can use the length property in the Buffer instance.

Let's say we have a buffer from the string Hello World! like this,

// Buffer from string Hello World!
const buff = Buffer.from("Hello World!", "utf-8");
Enter fullscreen mode Exit fullscreen mode

Now let's get its length using the property length in the buff object.

// Buffer from string Hello World!
const buff = Buffer.from("Hello World!", "utf-8");

// get the length of the Buffer
const length = buff.length;

console.log(length); // 12
Enter fullscreen mode Exit fullscreen mode

See this example live in repl.it.

Feel free to share if you found this useful 😃.


Top comments (0)