Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

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

WHY

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

WHAT
  • 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 typescript and run npx tsc --noEmit to type-check.

How it flows

1 · Describe the function Node 20, no packages, example 2 · AI writes code read every line 3 · Run it node slugify.js 4 · Ask for tests node:test + assert 5 · Run tests node --test = green

Install and check

HOW
  1. Install Node.js LTS from nodejs.org (or nvm install --lts).
  2. Check it: node -v should show v20 or newer.
  3. Make a folder: mkdir slug-demo && cd slug-demo.
  4. Optional: npm init -y to create package.json. Set "test": "node --test" under scripts.

Step 1 — the prompt

COPY THIS 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-2026

Step 4 — ask for tests, then run them

FOLLOW-UP PROMPT

Write tests with node:test and node:assert for slugify: a normal title, accents with extra spaces, and a non-string input.

slugify.test.js
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 0

TypeScript 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

  1. Tell the AI your module style: CommonJS (require) or ES modules (import, "type": "module" in package.json). Mixing them is a common AI mistake.
  2. Project rules file: add “Node 20, node:test, no new npm packages without asking” to .github/copilot-instructions.md or CLAUDE.md.
  3. Lint: npm init @eslint/config@latest then npx 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 of async/await.
  • Browser vs Node — document does not exist in Node; require does 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

SMART AI USE

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

INTERVIEW CARDAI Coding

How do you prove AI JS code works?

Run it with node.
Write node:test tests and run node --test.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

JavaScript vs TypeScript with AI

JS: fast to start, errors show at run time.
TS: types catch AI mistakes early with tsc --noEmit.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

JS with AI in 3

Say Node version + module style + no packages.
One example in the prompt.
node --test = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding