4321 is Astro
Astro’s default port is 4321. Like Vite, this project is trying to convey its (rocket-launch) branding, even in this small detail. ...
Astro’s default port is 4321. Like Vite, this project is trying to convey its (rocket-launch) branding, even in this small detail. ...
Node Timeout timers have a private boolean attribute called _destroyed. It tells you if the function has been cleared: timer._destroyed; ...
Today I used JavaScript to mass-unsubscribe from a website’s email notifications. Here’s the code, followed by what I did and why. document .querySelectorAll('[role="switch"]') .forEach((element) => element.click()); ...
Reflection is an important tool in programming; it lets us examine a language’s structure. Can we reflect in JavaScript? Sort of. Some functions can be reflected with toString(): const one = () => 1; // undefined one.toString(); // '() => 1' Very cool! But native functions only return this: > Math.max.toString() 'function max() { [native code] }' You must read the ECMAScript specification to know what the function is required to do. The actual implementation is written in the JavaScript engine, which for Node is Torque, which is why Node can’t show it via toString().
A vital testing skill is focusing a test. When iterating on a failing test, we want that test under microscope. The way I do this in JavaScript is .only: import {describe, expect, test} from 'vitest'; import {sum} from './sum.js'; test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); }); describe.only('adding fours', () => { test('adds 4 + 4 to equal 8', () => { expect(sum(4, 4)).toBe(8); }); }); ...
JSDoc has a @deprecated tag that you can use in TypeScript, too. Here’s an example. ...
When upgrading a dependency for a security issue, we might upgrade and move on: npm install <dependency>@<version> But wait! Was every <dependency> version upgraded, or just the version in package.json? Check with: npm list // or npm ls ...
After version v12.17.0, Node’s REPL provides a useful preview effect. In this example, 2 is shown in gray before I hit enter. > 1 + 1 2 But what if I don’t want that? Here’s a file called repl that disables it as an option: #!/usr/bin/env node const repl = require('node:repl'); repl.start({preview: false}); Then: $ ./repl > 1 + 1 No preview. Docs
With my Node versions managed via NVM, I want to be on the latest LTS. You can ensure that happens with these commands: $ nvm install --lts $ nvm alias default lts/* default -> node (-> v24.16.0) ...
What I call “wraparound” are repeatable indices on an array. The common use case is a carousel. Here’s how it’s done: const items = ['first', 'middle', 'last']; const i = 0; // Our iterable index // "Next" -> const nextIndex = (i + 1) % items.length; // <- "Previous" const prevIndex = (i - 1 + items.length) % items.length;