Home > Blockchain >  How to read .txt file in Javascript
How to read .txt file in Javascript

Time:12-11

I have been learning JS and I have a problem:

How can i assigned data from input.txt to variable a Help me.

I've searched online and YouTube, but I still can't find a answer

screenshot of two files open in editor

CodePudding user response:

Assuming you are using HTML, you can use fetch:

fetch('input.txt')
  .then((response) => response.text())
  .then(function(data) {
    console.log(data);
  });

You could also use this in an async context:

async function getInput() {
  let res = await fetch("input.txt");
  let text = await res.text();
  return text;
}

CodePudding user response:

const { readFileSync } = require('fs')
const contents = readFileSync(filename, 'utf-8')
  • Related