# .txt

> The `.txt` is a plain text file format that contains no formatting, often used for storing simple text data.

## How do I create and download a .txt file with JavaScript?

The basic file format that stores raw text without any formatting. It's commonly used for storing notes, logs and simple text data that need to be easily readable and editable across multiple platforms.

- Topic: Document & Data Formats
- Type: format

## Example

Wrong:

```js
const blob = new Blob([text]);
location.href = URL.createObjectURL(blob);
```

Right:

```js
const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = Object.assign(document.createElement('a'), { href: url, download: 'notes.txt' });
a.click();
URL.revokeObjectURL(url);
```

A typed UTF-8 blob and a link with `download` save a named file instead of opening it, and the URL is freed after.

## Common mistakes

- Text turning into garbled characters because the file or server uses the wrong encoding.
- Mixing Windows and Unix line endings, which shows up as diffs on every line.

## Key points

- The `Blob` type is 'text/plain;charset=utf-8' so non-English text keeps its characters.
- The object URL is released with `URL.revokeObjectURL()` after the download starts.
- Servers send `.txt` files with 'text/plain; charset=utf-8' as the `Content-Type`.

## Interview questions

### What encoding should a .txt file use?

Use UTF-8. It covers every language and is the default for most editors, servers and tools.

### What is the difference between LF and CRLF line endings?

Unix and macOS use LF, while Windows tools often use CRLF. Pick one per project, usually LF, and enforce it in the editor and Git.

## Related cards

- [UTF-8](https://wonderium.dev/web/utf-8.md)
- [robots.txt](https://wonderium.dev/web/robots-txt.md)
- [llms.txt](https://wonderium.dev/ai/llms-txt.md)
- [.csv](https://wonderium.dev/engineering/csv.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/txt
- Last updated: 2026-11-25
