Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Node Js Function Declaration and Expression

I just started to learn Node Js then I got the following code in the tutorial

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const questions = [
  "What is your name? ",
  "Where do you live? ",
  "What are you going to do with node js? "
];

const collectAnswers = (questions, done) => {
  const answers = [];
  const [firstQuestion] = questions;

  const questionAnswered = answer => {
    answers.push(answer);
    if (answers.length < questions.length) {
      rl.question(questions[answers.length], questionAnswered);
    } else {
      done(answers);
    }
  };

  rl.question(firstQuestion, questionAnswered);
};

collectAnswers(questions, answers => {
  console.log("Thank you for your answers. ");
  console.log(answers);
  process.exit();
});

The code has the following result

What is your name? a
Where do you live? b
What are you going to do with node js? c
Thank you for your answers.
[ 'a', 'b', 'c' ]

As far as I understand that the variable collectAnswer somehow inject the function declared below to the second parameter (done). Can someone explain what is actually happening behind the scene? How can the function declared below injected to the variable with the same name with it? Any term this pattern actually called?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Functions are objects in JavaScript, unlike some other languages, so you can pass them into functions as arguments and the functions can receive them as parameters. This is done a lot so that the function you're calling can "call back" to your code (the function you're passing in). A function used this way is often called a "callback." A function like collectAnswers that accepts a function it's going to call this way is often described as a function that "accepts a callback."

In that code, there's a function called collectAnswers and it accepts two parameters, questions and done:

const collectAnswers = (questions, done) => {
//                      ^^^^^^^^^−−^^^^−−−−−−−−−−−−−−−− parameters
    // ...
};

At the end, the code calls collectAnswers, passing in an array of questions and new function created inline as the second argument (I've put the two arguments on separate lines for clarity):

collectAnswers(
    questions,                                          // 1st argument

    answers => {                                        //
        console.log("Thank you for your answers. ");    //
        console.log(answers);                           // 2nd argument
        process.exit();                                 //
    }                                                   //
);

That does the same thing as this, except for the callback constant:

const callback = answers => {
    console.log("Thank you for your answers. ");
    console.log(answers);
    process.exit();
};
collectAnswers(questions, callback);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!