0

I just started learning Javascript and I can't seem to wrap my head around what is happening here. Can someone explain why this piece of code correctly outputs data from my file:

var fs = require('fs');

var file = fs.readFile('listofwords.csv', function(err, data) {
    output = data.toString()
    console.log(output)
})

But when I modify the code to output the file outside of it's scope it gives me undefined:

var fs = require('fs');

var file = fs.readFile('randomwords.csv', function(err, data) {
    output = data.toString()
    return output
})

console.log(file)

If you can point me to the right direction will be much appreciated! Thank you!

Jay
  • 65
  • 1
  • 5

0 Answers0