DEV Community

Kyle Schwartz
Kyle Schwartz

Posted on • Updated on

Useful Bits of Code From Around The Web

Reading Files in JS

Text File

fetch('file.txt')
  .then(response => response.text())
  .then(text => console.log(text))
  // outputs the content of the text file
Enter fullscreen mode Exit fullscreen mode

JSON File

fetch('file.json')
  .then(response => response.json())
  .then(jsonResponse => console.log(jsonResponse))     
   // outputs a javascript object from the parsed json
Enter fullscreen mode Exit fullscreen mode

Top comments (0)