Style - How to write meaningful comments
Text files are the simplest format: just plain characters with newlines.
import fs from 'node:fs';
// Read all lines
const content = fs.readFileSync('data.txt', 'utf8');
const lines = content.split('\\n');
// Process each line
for (const line of lines) {
console.log(line.trim());
}
// Filter lines
const nonEmpty = lines.filter(line => line.trim().length > 0);
// Find specific content
const matchingLines = lines.filter(line => line.includes('error'));
const lines = ['Header', 'Line 1', 'Line 2', 'Footer'];
fs.writeFileSync('output.txt', lines.join('\\n'));
// Append lines
const newLines = ['Additional 1', 'Additional 2'];
fs.appendFileSync('output.txt', '\\n' + newLines.join('\\n'));
Example: Log Parser
import fs from 'node:fs';
const logs = fs.readFileSync('app.log', 'utf8').split('\\n');
const errors = logs.filter(line => line.includes('ERROR'));
console.log(`Found ${errors.length} errors:`);
for (const error of errors) {
console.log(error);
}
<aside> ⌨️
Hands on: Create a text file with 10 lines of mixed content. Write code to read it, remove empty lines, and save only lines containing a specific word.
</aside>
CSV (Comma-Separated Values) stores tabular data in plain text. Each line is a row, commas separate columns.
import fs from 'node:fs';
const csv = fs.readFileSync('data.csv', 'utf8');
const lines = csv.split('\\n');
// Get headers
const headers = lines[0].split(',');
// Get data rows
const rows = [];
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const values = lines[i].split(',');
const row = {};
for (let j = 0; j < headers.length; j++) {
row[headers[j].trim()] = values[j].trim();
}
rows.push(row);
}
}
console.log(rows);
Example CSV file (users.csv):
name,age,city
Alice,28,Amsterdam
Bob,32,Rotterdam
Charlie,25,Utrecht
Parsed result:
[
{ name: 'Alice', age: '28', city: 'Amsterdam' },
{ name: 'Bob', age: '32', city: 'Rotterdam' },
{ name: 'Charlie', age: '25', city: 'Utrecht' }
]
import fs from 'node:fs';
const users = [
{ name: 'Alice', age: 28, city: 'Amsterdam' },
{ name: 'Bob', age: 32, city: 'Rotterdam' }
];
// Create headers
const headers = Object.keys(users[0]);
const csvHeaders = headers.join(',');
// Create rows
const csvRows = users.map(user => {
return headers.map(header => user[header]).join(',');
});
// Combine and write
const csv = [csvHeaders, ...csvRows].join('\\n');
fs.writeFileSync('users.csv', csv);
Output (users.csv):
name,age,city
Alice,28,Amsterdam
Bob,32,Rotterdam
JSON (JavaScript Object Notation) is perfect for structured data. It's JavaScript's native format.
import fs from 'node:fs';
// Read and parse JSON
const jsonString = fs.readFileSync('data.json', 'utf8');
const data = JSON.parse(jsonString);
console.log(data);
console.log(data.name); // Access properties directly
Example JSON file:
{
"name": "Alice",
"age": 28,
"hobbies": ["reading", "coding"],
"address": {
"city": "Amsterdam",
"country": "Netherlands"
}
}
const user = JSON.parse(fs.readFileSync('user.json', 'utf8'));
console.log(user.name); // Alice
console.log(user.hobbies[0]); // reading
console.log(user.address.city); // Amsterdam
import fs from 'node:fs';
const data = {
users: [
{ id: 1, name: 'Alice', active: true },
{ id: 2, name: 'Bob', active: false }
],
timestamp: new Date().toISOString()
};
// Convert to JSON string (formatted)
const jsonString = JSON.stringify(data, null, 2);
fs.writeFileSync('data.json', jsonString);
JSON.stringify() parameters:
null)// Compact (one line)
const compact = JSON.stringify(data);
// {"users":[{"id":1,"name":"Alice"}]}
// Formatted (readable)
const formatted = JSON.stringify(data, null, 2);
// {
// "users": [
// {
// "id": 1,
// "name": "Alice"
// }
// ]
// }
import fs from 'node:fs';
// Read existing data
const users = JSON.parse(fs.readFileSync('users.json', 'utf8'));
// Modify
users.push({ id: 3, name: 'Charlie', age: 25 });
// Write back
fs.writeFileSync('users.json', JSON.stringify(users, null, 2));
<aside> ⌨️
Hands on: Create a JSON file with an array of products. Read it, add a new product, filter out products under $10, and save the result.
</aside>
| Format | Best For | Pros | Cons |
|---|---|---|---|
| TXT | Simple logs, notes | Human readable, simple | No structure |
| CSV | Tabular data, spreadsheets | Compact, Excel-compatible | Limited data types, no nesting |
| JSON | Structured data, configs | Rich types, nested data, JavaScript native | Verbose, larger files |
<aside> ⚠️
Real applications often use all three formats: JSON for configuration, CSV for data export, and TXT for logs. Master all three to handle any data format you encounter.
</aside>
Text Files:
\\n to get lines\\n to write linesCSV Files:
, to get columnsJSON Files:
JSON.parse() to readJSON.stringify() to write
Found a mistake or have a suggestion? Let us know in the feedback form.