How to Code in JavaScript/TypeScript Using AI
Use an AI assistant to write a small JavaScript function, run it with Node.js, and prove it works with Node's built-in test runner. No extra packages needed.
In short: say the Node version and “no npm packages”, give one example, ask for node:test tests, run node --test.
Why JavaScript with AI
JavaScript runs websites, servers (Node.js) and apps. There are many ways to do one thing, and old examples are everywhere. A clear prompt keeps the AI on modern, simple code.
What you need
- Node.js 20 or newer: includes
node --test, the built-in test runner. - An editor + assistant: VS Code with GitHub Copilot or Claude Code, or WebStorm with Copilot.
- TypeScript (optional):
npm i -D typescriptand runnpx tsc --noEmitto type-check.
How it flows
Install and check
- Install Node.js LTS from nodejs.org (or
nvm install --lts). - Check it:
node -vshould show v20 or newer. - Make a folder:
mkdir slug-demo && cd slug-demo. - Optional:
npm init -yto createpackage.json. Set"test": "node --test"under scripts.
Step 1 — the prompt
Write a Node.js 20 CommonJS module with slugify(title). No npm packages. Lowercase, remove accents, turn anything that is not a-z or 0-9 into one dash, trim dashes at the ends. Throw TypeError if title is not a string. Example: slugify("Hello, World! 2026") returns "hello-world-2026". Only return the code.
Step 2 — the code (tested here)
slugify.js// Turn a title into a URL slug. Built with an AI assistant, then tested.
function slugify(title) {
if (typeof title !== "string") throw new TypeError("title must be a string");
return title
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
module.exports = { slugify };
if (require.main === module) console.log(slugify(process.argv[2] || "Hello, World! 2026"));Step 3 — run it
node slugify.js "Hello, World! 2026"
# -> hello-world-2026Step 4 — ask for tests, then run them
Write tests with node:test and node:assert for slugify: a normal title, accents with extra spaces, and a non-string input.
const test = require("node:test");
const assert = require("node:assert");
const { slugify } = require("./slugify");
test("basic title", () => {
assert.strictEqual(slugify("Hello, World! 2026"), "hello-world-2026");
});
test("accents and extra spaces", () => {
assert.strictEqual(slugify(" Café Menu "), "cafe-menu");
});
test("rejects non-string", () => {
assert.throws(() => slugify(42), TypeError);
});node --test
# -> tests 3, pass 3, fail 0TypeScript version (same idea)
Ask: “Convert slugify.js to TypeScript with strict types.” The function signature becomes:
export function slugify(title: string): string { ... }Then run npx tsc --noEmit to check types before you run it.
Settings and config
- Tell the AI your module style: CommonJS (
require) or ES modules (import,"type": "module"in package.json). Mixing them is a common AI mistake. - Project rules file: add “Node 20, node:test, no new npm packages without asking” to
.github/copilot-instructions.mdorCLAUDE.md. - Lint:
npm init @eslint/config@latestthennpx eslint ..
Watch out (common AI mistakes in JS)
- Fake or unneeded npm packages — check on npmjs.com before installing. Typo-named packages can be malware.
- Old patterns —
var, callbacks instead ofasync/await. - Browser vs Node —
documentdoes not exist in Node;requiredoes not exist in the browser. ==vs===— ask for strict equality.
Quick quiz
1. Which command runs Node's built-in tests?
node --test.
2. Why say “no npm packages”?
Fewer dependencies, and no risk of fake or harmful packages.
3. What does slugify("Hello, World! 2026") return?
hello-world-2026.
4. How do you type-check TypeScript without building?
npx tsc --noEmit.
Save tokens and money
Paste only the function, not node_modules or a lock file. Never let the AI read package-lock.json unless needed, it is huge. Ask for one function plus tests, then the next.
Save these cards
How do you prove AI JS code works?
node --test.JavaScript vs TypeScript with AI
tsc --noEmit.