JavaScript Program to write data in a text File Last Updated : 21 Jun, 2025 Suggest changes Share Like Article Like Report Here are two simple methods for writing data to a text file using JavaScript.1. Using the Blob ObjectThe Blob object is a built-in JavaScript feature that allows you to create file-like objects of immutable raw data. Using a Blob, you can easily generate text files directly in the browser. Here's how to use it:Steps:Create a Blob object containing the data you want to write to a text file.Use the URL.createObjectURL() method to create a downloadable link.Trigger the file download automatically when the user clicks a button. HTML <html> <head> <title>Write Data to a Text File</title> <style> body { display: flex; justify-content: center; align-items: flex-start; height: 100vh; margin: 0; font-family: Arial, sans-serif; text-align: center; padding-top: 50px; } textarea { margin-bottom: 10px; padding: 10px; font-size: 16px; width: 300px; } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } </style> </head> <body> <div> <h2>JavaScript Program to Write Data to a Text File</h2> <textarea id="textData" rows="6" cols="50" placeholder="Enter text to save"></textarea><br><br> <button onclick="downloadTextFile()">Download Text File</button> </div> <script> function downloadTextFile() { var text = document.getElementById("textData").value; var blob = new Blob([text], { type: 'text/plain' }); var link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'myTextFile.txt'; link.click(); } </script> </body> </html> OutputIn this exampledownloadTextFile() retrieves the text from the <textarea>.A Blob is created with the text, and a temporary download link is generated.The link triggers the download of the text file named myTextFile.txt.2. Using FileSaver.js LibraryFor more complex use cases or browser compatibility, we might prefer using a library like FileSaver.js, which simplifies saving files in JavaScript. It provides a more robust solution for triggering file downloads.Steps:Include the FileSaver.js library in your project.Use the saveAs() function to trigger the download of the file. HTML <html> <head> <style> body { display: flex; justify-content: center; align-items: flex-start; height: 100vh; margin: 0; font-family: Arial, sans-serif; text-align: center; padding-top: 50px; } textarea { margin-bottom: 10px; padding: 10px; font-size: 16px; width: 300px; } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } </style> </head> <body> <div> <h2>JavaScript Program to Write Data to a Text File</h2> <textarea id="textData" rows="6" placeholder="Enter text to save"></textarea><br><br> <button onclick="downloadTextFile()">Download Text File</button> </div> <script> function downloadTextFile() { var text = document.getElementById("textData").value; var blob = new Blob([text], { type: 'text/plain' }); var link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'myTextFile.txt'; link.click(); } </script> </body> </html> OutputIn this exampleHTML: Displays a heading, a textarea for input, and a button.JavaScript: When the button is clicked, the text from the textarea is saved as a .txt file and downloaded automatically.ConclusionWriting data to a text file using JavaScript is an easy and effective way to allow users to download content directly from the web. By using the Blob object or the FileSaver.js library, you can easily create files in the browser and provide them for download. Advertise with us Next Article JavaScript Program to write data in a text File I imdhruvgupta Follow Similar Reads Java Program to Write Data to Temporary File A Temporary file or a Temp file is a file that is created in order to hold information for some time while a file is being created or modified. After the successful execution of the program or after the program is closed, the temporary file is deleted. Other advantages of having a temporary file are 3 min read Program to convert Byte Array to Writer in Java References: Writer Class Approach: Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used. Below is the implementation of the above appr 2 min read Reading and Writing lists to a file in Python Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the 5 min read How to write Into a File in PHP ? In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file 2 min read Reading and Writing Data to Excel File in Java using Apache POI In Java, reading an Excel file is not similar to reading a Word file because of cells in an Excel file. JDK does not provide a direct API to read data from Excel files for which we have to toggle to a third-party library that is Apache POI. Apache POI is an open-source java library designed for read 5 min read Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2018 JavaScript-Program +1 More Like