How to Write Text File JavaScript W3Schools Tutorial

Logan Jul 13, 2026

In the dynamic world of web development, JavaScript plays a pivotal role, offering extensive capabilities that transcend beyond just client-side scripting. One such functionality is writing to files, which might seemventional but is incredibly useful. Let's explore how you can write text to a file using JavaScript, guided by the esteemed learning resource, W3Schools.

Javascript Cheet cheet
Javascript Cheet cheet

Before we dive into the JavaScript code, it's essential to understand that you can't write directly to the user's file system due to security restrictions. Instead, you'll write to the server-side, which can then be saved or processed as needed. Now, let's delve into how you can achieve this.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Using Server-Side JavaScript

The most common way to write to a file using JavaScript is on the server-side, as it has access to the file system. You'll need a server-side JavaScript environment, such as Node.js, which can interact with the local file system.

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

Let's start with a simple example of writing to a file using Node.js and the built-in 'fs' module.

Writing to a File Using fs.writeFile()

JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“

The 'fs' module provides a method, 'writeFile()', which writes data to a file, replacing its contents if the file already exists. Here's a basic example:

const fs = require('fs');
fs.writeFile('example.txt', 'Hello, World!', (err) => {
if (err) throw err;
console.log('The file has been saved!’);
});

In this example, a new file named 'example.txt' is created (or the existing one is rewritten), containing the string 'Hello, World!'.

JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘

Appending to an Existing File Using fs.appendFile()

If you want to add data to an existing file without replacing its contents, use 'fs.appendFile()'.

const fs = require('fs');
fs.appendFile('example.txt', ' Welcome!', (err) => {
if (err) throw err;
console.log('Text has been appended!’);
});

JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming

In this case, the string ' Welcome!' will be added to the 'example.txt' file.

Writing to a File in a Browser Environment (Client-Side)

an info sheet with different types of web pages
an info sheet with different types of web pages
JavaScript | Chapter 2 πŸ“–| Part 1 ✨
JavaScript | Chapter 2 πŸ“–| Part 1 ✨
10 Must-Know JavaScript Shorthand Techniques
10 Must-Know JavaScript Shorthand Techniques
πŸ”₯ JavaScript Chapter 1 Notes 2026 πŸ“’βœ¨
πŸ”₯ JavaScript Chapter 1 Notes 2026 πŸ“’βœ¨
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
Javascript Add Text To Image
Javascript Add Text To Image
JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Basics | Chapter 1 πŸ“– | Part 2
JavaScript Basics | Chapter 1 πŸ“– | Part 2
javascript cheat sheet web development
javascript cheat sheet web development
a table that has some type of text on it, including the names and abbreviations
a table that has some type of text on it, including the names and abbreviations
Javascript Loops & Iterations
Javascript Loops & Iterations
Html notes
Html notes
a piece of paper that has some type of text on it with red marker marks
a piece of paper that has some type of text on it with red marker marks
Javascript Window.print()
Javascript Window.print()
Basic Structure of JavaScript (Beginner-Friendly Visual Guide)
Basic Structure of JavaScript (Beginner-Friendly Visual Guide)
How To Create Rainbow Text Using CSS JS
How To Create Rainbow Text Using CSS JS
a blackboard with text and diagrams on it
a blackboard with text and diagrams on it
Master JavaScript Variables: A Beginner’s Guide πŸš€
Master JavaScript Variables: A Beginner’s Guide πŸš€
an image of a web page with text
an image of a web page with text
a piece of paper with writing on it that says string method in jaascript
a piece of paper with writing on it that says string method in jaascript

While you can't directly write to the user's file system due to security concerns, you can still create and save data locally on the client-side using the File System Access API. This API enables web applications to read and write files on the user's device.

To use this API, you'll need to check if the user's browser supports it, as it's still experimental at the time of writing.

Using the File System Access API

Here's a basic example of creating a new file and writing data to it using the File System Access API:

async function writeToFile() {
const fs = await navigator.filesystem.getFileSystem('new');
const fileHandle = await fs.open('example.txt', 'utf-8');
await fileHandle.write('Hello, World!');
}

In this example, a new file called 'example.txt' is created, and the string 'Hello, World!' is written to it.

And there you have it! You've now explored how to write text to a file using JavaScript on both server-side and client-side environments. This skill is essential for building dynamic and interactive web applications. Happy coding!