Why JavaScript is Your Key Ally in Automation
Have you ever caught yourself thinking that you spend hours on repetitive tasks: renaming hundreds of files, copying data from spreadsheets, sending dozens of emails, or checking if a website is down? If so, this article is for you.
JavaScript has long ceased to be just a "language for button animations." With the advent of Node.js and powerful libraries, it has become a universal tool for task automation. And yes, even if you are a Python developer, JS can become your secret weapon: it works everywhere — in the browser, on the server, in the cloud, and even on a Raspberry Pi.
In this article, we will break down real-world automation scenarios in JavaScript that will save you dozens of hours. No fluff — just code and practice.
1. Browser Automation: Puppeteer and Playwright
Imagine you need to collect prices from 500 pages of an online store, but there is no API. Manual copy-paste? No, thanks. Puppeteer comes to the rescue — a library for controlling Chromium.
Example: Parsing News Headlines
const puppeteer = require('puppeteer');
(async () => { const browser = await puppeteer.launch({ headless: true }); const page = await browser.newPage(); await page.goto('https://news.ycombinator.com', { waitUntil: 'networkidle2' }); const titles = await page.evaluate(() => { const items = document.querySelectorAll('.titleline > a'); return Array.from(items).map(item => item.innerText); }); console.log('Latest news:', titles.slice(0, 10)); await browser.close();})();This script opens Hacker News, collects the headlines of the first 10 news items, and prints them to the console. In real life, you could add saving to CSV, sending to Telegram, or writing to a database.
Playwright — An Alternative with Superpowers
If you need to work with multiple browsers (Chrome, Firefox, Safari) or emulate mobile devices, use Playwright. It is faster, more stable, and can intercept requests.
2. File Handling: Renaming, Sorting, Cleaning
Do you have a folder with 1000 photos named like "IMG_20250101_123456.jpg"? Want to organize them? The built-in fs (File System) module in Node.js can handle this in seconds.
Example: Bulk File Renaming
const fs = require('fs');const path = require('path');
const directory = './photos';
fs.readdir(directory, (err, files) => { if (err) throw err; files.forEach((file, index) => { const ext = path.extname(file); const newName = `photo_${String(index + 1).padStart(4, '0')}${ext}`; fs.rename( path.join(directory, file), path.join(directory, newName), (err) => { if (err) console.error(`Error with file ${file}:`, err); else console.log(`${file} → ${newName}`); } ); });});You can easily modify the script: add a filter by extension, extract the date from EXIF, rename according to a template. This is the foundation upon which dozens of utilities are built.
3. Automating Work with CSV and Excel (Without an Office Suite)
Business tasks often require processing spreadsheets. For CSV, use the built-in module or the csv-parser library. For Excel, use xlsx (SheetJS).
Example: Reading CSV and Creating a Report
const fs = require('fs');const csv = require('csv-parser');const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const results = [];
fs.createReadStream('orders.csv') .pipe(csv()) .on('data', (row) => { if (parseFloat(row.total) > 100) { results.push({ orderId: row.id, client: row.name, sum: row.total }); } }) .on('end', () => { const csvW