Based on http://html5-demos.appspot.com/static/a.download.html:

var fileContent = "My epic novel that I don't want to lose.";
var bb = new Blob([fileContent ], { type: 'text/plain' });
var a = document.createElement('a');
a.download = 'download.txt';
a.href = window.URL.createObjectURL(bb);
a.click();

Modified the original fiddle: http://jsfiddle.net/9av2mfjx/

Answer from Stanislav on Stack Overflow
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Save a File With JavaScript | Envato Tuts+
June 19, 2022 - We create an object that contains different options for our file picker that shows up when we call the showFilePicker() method. We can suggest a name to save the file here and also pass an array of allowed file types to save.
🌐
Jotform
jotform.com › home › advice › html5: filesystem api - create files and store them locally using javascript and webkit
HTML5: FileSystem API - Create Files and Store Them Locally Using JavaScript and Webkit | The Jotform Blog
April 3, 2023 - The FileSystem-API allows the creation of files and folders as well as their local storage using JavaScript. Files can be simple text files, but even more complex files such as images are possible. Modern Webkit browsers with HTML5 support are already able to handle the FileSystem-API. We show you how you can benefit from the new possibilities. ... To be able to save files and folders from inside the web browser to the local hard drive you’ll need access to the filesystem.
Videos
February 8, 2023
🌐
4umi
4umi.com › web › javascript › filewrite.php
Writing to the local file system - 4umi useful Javascript
Fatal error: Uncaught Error: Call to undefined function split() in /var/www/vip15/sites/vip2072981/httpd/htdocs/indew.inc:10 Stack trace: #0 /var/www/vip15/sites/vip2072981/httpd/htdocs/web/javascript/filewrite.php(1): include() #1 {main} thrown in /var/www/vip15/sites/vip2072981/httpd/htd...
🌐
Stack Overflow
stackoverflow.com › questions › 11071473 › how-can-javascript-save-to-a-local-file
jquery - How can JavaScript save to a local file? - Stack Overflow

Based on http://html5-demos.appspot.com/static/a.download.html:

var fileContent = "My epic novel that I don't want to lose.";
var bb = new Blob([fileContent ], { type: 'text/plain' });
var a = document.createElement('a');
a.download = 'download.txt';
a.href = window.URL.createObjectURL(bb);
a.click();

Modified the original fiddle: http://jsfiddle.net/9av2mfjx/

Answer from Stanislav on stackoverflow.com
🌐
Robkendal
robkendal.co.uk › blog › 2020-04-17-saving-text-to-client-side-file-using-vanilla-js
Saving text to a client-side file using vanilla JS
April 17, 2020 - Want to save files to the client using JavaScript? Let's look at how to save a file client-side using s simple handful of vanilla JavaScript
🌐
GitHub
gist.github.com › liabru › 11263260
Save a text file locally with a filename, by triggering a download in JavaScript · GitHub
A solution is to probably stick with manually downloading through the element appended somewhere. function appendDownloadableText(filename, string) { const data = new Blob([string]); const url = URL.createObjectURL(data); const a = ...
🌐
Stack Overflow
stackoverflow.com › questions › 2897619 › using-html5-javascript-to-generate-and-save-a-file
html - Using HTML5/JavaScript to generate and save a file - Stack Overflow

Simple solution for HTML5 ready browsers...

function download(filename, text) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);

    if (document.createEvent) {
        var event = document.createEvent('MouseEvents');
        event.initEvent('click', true, true);
        pom.dispatchEvent(event);
    }
    else {
        pom.click();
    }
}

Usage

download('test.txt', 'Hello world!');
Answer from Matěj Pokorný on stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 18690450 › how-to-generate-and-prompt-to-save-a-file-from-content-in-the-client-browser
javascript - How to generate and prompt to save a file from content in the client browser? - Stack Overflow

This "FileSaver" library may help. If you want it to be reasonably cross-browser, you'll also need this to implement the W3C Blob API in places it's not already implemented. Both respect namespaces, and are completely framework agnostic, so don't worry about naming issues.

Once you've got those included, and as long as you're only saving text files, you should be able to

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

Note that the first argument to new Blob has to be a list of strings, and that you're expected to specify the filename. As in, the user will see this file being downloaded locally, but won't be able to name it themselves. Hopefully they're using a browser that handles local filename collisions...

Answer from David Morabito on stackoverflow.com
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › how can i save a json value from html/javascript in a json file locally?
r/learnjavascript on Reddit: How can I save a json value from HTML/JavaScript in a json file locally?
There's not a very clean way for the browser to save a file to the users filesystem that your site can access without user interaction. This is for security purposes. I believe the fs model you are talking about is for node, which is JavaScript running on the backend. As an alternative for persistent data I would recommend cookies or local storage , the latter being more straight forward.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-and-save-text-file-in-javascript
How to Create and Save text file in JavaScript?
We have added the CDN of the ... which creates the blob object of the sentence ?This is a simple file content' and executes the ?saveAs()''function to save the text file....
🌐
Reddit
reddit.com › r/learnjavascript › how do i save/load local files instead of local storage with pure js?
r/learnjavascript on Reddit: How do I save/load local files instead of local storage with pure js?
Is this for a personal project only meant for your use? Or for others to use? What kind of data? In general, you don’t want to give users the ability to write directly to the file system that is hosting your site. That’s a recipe for disaster. And in any case, I don’t believe there is an easy way. You can have the user download a file and then reupload it every time they use it, but that isn’t practical nor good UX. If you’re just looking to store data, you can look into something like firebase and firestore, if you don’t want to build your own backend. or literally any sort of backend with a database otherwise. If you really want to be able to write to the user’s file system, you can look into distributing your app with electron, but if this is just a basic website and not an app, that’s not practical.
🌐
Stack Overflow
stackoverflow.com › questions › 19119040 › how-do-i-save-and-restore-a-file-object-in-local-storage
javascript - How do I save and restore a File object in local storage - Stack Overflow

You cannot serialize file API object.

Not that it helps with the specific problem, but ... Although I haven't used this, if you look at the article it seems that there are ways (although not supported yet by most browsers) to store the offline image data to some files so as to restore them afterward when the user is online (and not to use localStorage)

Answer from George Mavritsakis on stackoverflow.com
🌐
SitePoint
sitepoint.com › javascript
How do I write to a local file using javascript - JavaScript - SitePoint Forums | Web Development & Design Community
March 24, 2020 - Greetings to the community. Hope you’re all doing OK. I’m using javascript for programming on my own computer (not for a webpage on the net) so the usual security concerns are not an issue. How do I write to a file on my own computer? I can write and retrieve a string using local storage e.g. window.localStorage.setItem(‘games’, games) and window.localStorage.getItem(‘games’), but I’d like to be able to write strings to a file on my computer.
🌐
Stack Overflow
stackoverflow.com › questions › 38462894 › how-to-create-and-save-file-to-local-filesystem-using-angularjs
javascript - How to create and save file to local fileSystem using AngularJS? - Stack Overflow

Save to filesystem

Have a look at angular-file-saver

Or use the following code as a reference in saving a BLOB. Where the blob object is generated from a JSON Object. But extration to a TEXT file is also possible.

    // export page definition to json file
    $scope.exportToFile = function(){
        var filename = 'filename'       
        var blob = new Blob([angular.toJson(object, true)], {type: 'text/plain'});
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(blob, filename);
        } else{
            var e = document.createEvent('MouseEvents'),
            a = document.createElement('a');
            a.download = filename;
            a.href = window.URL.createObjectURL(blob);
            a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
            e.initEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            a.dispatchEvent(e);
            // window.URL.revokeObjectURL(a.href); // clean the url.createObjectURL resource
        }
    }

Using LocalStorage

Saving to localStorage:

window.localStorage.setItem('key', value);

Getting from localStorage

window.localStorage.getItem('key');

Delete key from localStorage

window.localStorage.removeItem('key');

Or using the AngularJS module 'ngStorage'

Browser compatibility

Chrome - 4    
Firefox (Gecko) - 3.5    
Internet Explorer - 8    
Opera - 10.50    
Safari (WebKit) - 4

See live example (credits to @cOlz)

https://codepen.io/gMohrin/pen/YZqgQW

Answer from daan.desmedt on stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 21012580 › is-it-possible-to-write-data-to-file-using-only-javascript
html - Is it possible to write data to file using only JavaScript? - Stack Overflow

You can create files in browser using Blob and URL.createObjectURL. All recent browsers support this.

You can not directly save the file you create, since that would cause massive security problems, but you can provide it as a download link for the user. You can suggest a file name via the download attribute of the link, in browsers that support the download attribute. As with any other download, the user downloading the file will have the final say on the file name though.

var textFile = null,
  makeTextFile = function (text) {
    var data = new Blob([text], {type: 'text/plain'});

    // If we are replacing a previously generated file we need to
    // manually revoke the object URL to avoid memory leaks.
    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    // returns a URL you can use as a href
    return textFile;
  };

Here's an example that uses this technique to save arbitrary text from a textarea.

If you want to immediately initiate the download instead of requiring the user to click on a link, you can use mouse events to simulate a mouse click on the link as Lifecube's answer did. I've created an updated example that uses this technique.

  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');

  create.addEventListener('click', function () {
    var link = document.createElement('a');
    link.setAttribute('download', 'info.txt');
    link.href = makeTextFile(textbox.value);
    document.body.appendChild(link);

    // wait for the link to be added to the document
    window.requestAnimationFrame(function () {
      var event = new MouseEvent('click');
      link.dispatchEvent(event);
      document.body.removeChild(link);
    });

  }, false);
Answer from Useless Code on stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-write-data-in-a-text-file
JavaScript Program to write data in a text File - GeeksforGeeks
02:32
It provides a more robust solution for triggering file downloads. ... Include the FileSaver.js library in your project. Use the saveAs() function to trigger the download of the file.
Published: July 11, 2025
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › 3e36f83c-4896-4f4f-9c02-bcb23e796182
How in JavaScript I can save a FileStreamResult to a local file ? | Microsoft Learn
With this code on the View, I am able to save string "aaa" to the user's local machine c:\myDir\myFile.txt: var Test = function() { var sSelection = getselection(); $.get('/Archive/Save', { 'selection': sSelection }, function (response) { var fso = new ActiveXObject("Scripting.FileSystemObject"), theFile = fso.CreateTextFile("C:\\myDir\\myFile.txt", true); theFile.Write('aaa'); //>> Successfully write "aaa" to c:\myDir\myFile.txt theFile.Close(); } ); }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › FileSystemWritableFileStream › write
FileSystemWritableFileStream: write() method - Web APIs | MDN
The following asynchronous function opens the 'Save File' picker, which returns a FileSystemFileHandle once a file is selected. From this, a writable stream is created using the FileSystemFileHandle.createWritable() method. A text string is then written to the stream, which is subsequently closed.
🌐
Stack Overflow
stackoverflow.com › questions › 48015124 › save-data-in-a-local-file-with-html-javascript › 48015176
Save data in a local file with HTML, Javascript? - Stack Overflow
You can't do that offline; HTML/JavaScript doesn't have the ability to write to the filesystem. You can create cookies or use .localStorage in JavaScript, but that would only be local to your browser and won't be visible on another computer.