CodePark/ questions / Buffers & binary data
START PRACTICINGPRACTICE
Backend question · Node.js › Buffers & binary data

What does this log?

const s = "héllo";
console.log(s.length, Buffer.from(s).length);
5 6

The answer is 5 6.

length on a string counts UTF-16 code units, which is 5 here. Buffer.from encodes as UTF-8 by default. In UTF-8, é takes 2 bytes, so the buffer is 6 bytes long.

This matters when you set
Content-Length or enforce byte limits. Use Buffer.byteLength(s) instead of s.length.