FAQ: Node.js Essentials - The Buffer Module

This community-built FAQ covers the “The Buffer Module” exercise from the lesson “Node.js Essentials”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Node.js

FAQs on the exercise The Buffer Module

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

After executing this code

const buffer1 = Buffer.from('hello');
const buffer2 = Buffer.from('world');

const bufferArray = [buffer1, buffer2];
const bufferConcat = Buffer.concat(bufferArray);

const bufferString = Buffer.toString(bufferConcat);

console.log(bufferString);

I got this output from the terminal:

$ node buffer.js
function Buffer(arg, encodingOrOffset, length) {
  showFlaggedDeprecation();
  // Common case.
  if (typeof arg === 'number') {
    if (typeof encodingOrOffset === 'string') {
      throw new ERR_INVALID_ARG_TYPE('string', 'string', arg);
    }
    return Buffer.alloc(arg);
  }
  return Buffer.from(arg, encodingOrOffset, length);
}

Shouldn’t it be helloworld? And why did it output this?

Hey! I’ve just completed the exercise and your code just has a typo at the fourth checkpoint, it should be:

const bufferString = bufferConcat.toString();

instead of:

const bufferString = Buffer.toString(bufferConcat);

As what you want to do is translate the previously created bufferConcat to a string.
Good luck!

2 Likes

Regarding this chapter on the Buffer module: I know this whole lesson is just supposed to cover some ‘essentials’ of Node without going too much in depth, but still, I found it fell short on explaining what would give everything in this exercise its sense: what’s a buffer, in what way it is relevant to Node.js and in what situation we would be brought to use the Buffer module. Like often, Codecademy does a good job of putting the cart before the horse by making us apply a bunch of methods while still leaving us clueless about when, where and why we should apply them…

The first sections of this page helped a bit, in case it can benefit someone else’s understanding: Node.js buffer: A complete guide - LogRocket Blog

8 Likes

Now I understand. Thanks!

1 Like

const bufferAlloc = Buffer.alloc(15, "b"); is useless here, no?

Agree completely, the syntax is not complex, but I feel lost in the deep end this entire lesson with literally zero context. Not sure how I can remember any of this if I have no way to visualize how it could be used.

1 Like

Why don’t we use the “new” keyword when creating a buffer instance here as we did when we created new event.EventEmitter()?