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

177
Views
How do I add data from a file to an array in JavaScript?

I'm just trying to add some text from a text file to an array. I can get all of the text to print individually, but I cannot seem to add it to the array. The code that I have now is:

const fs = require('fs');
let wordList = []

fs.readFile('answers.txt', 'utf-8', (err, data) => {
    if (err){
        console.log(err);
    }
    else{
        wordList.push(data);
    }
});

console.log(wordList);

For the final console.log, I was expecting a long array, but instead I get an empty array. The issue is with the push function, but aside from that, I do not know another way to add to an array.

about 4 years ago · Juan Pablo Isaza
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!