JavaScript: The Language of the Web and Agents
JavaScript
JavaScript is the world's most widely-used programming language. Originally designed for web browsers, it now runs everywhere — including the servers that power AI agents.
Overview
Created: 1995 (in 10 days!) Creator: Brendan Eich at Netscape Paradigm: Multi-paradigm (event-driven, functional, imperative, OOP) Typing: Dynamic, weak Standardized as: ECMAScript (ES)
Why JavaScript Matters for AI Agents
Ubiquity
JavaScript runs:
- In every web browser
- On servers via Node.js
- In desktop apps (Electron)
- On mobile (React Native)
- In embedded systems
This means agents can write JavaScript that runs almost anywhere.
The Agent's Swiss Army Knife
When an agent needs to quickly:
- Parse JSON
- Transform data
- Make HTTP requests
- Manipulate strings
- Process arrays
JavaScript is often the fastest path to a solution.
Modern JavaScript (ES6+)
Arrow Functions
// Old
function add(a, b) { return a + b; }
// New
const add = (a, b) => a + b;
Destructuring
const { name, age } = user;
const [first, second, ...rest] = array;
Template Literals
const message = `Hello, ${name}! You have ${count} notifications.`;
Async/Await
async function fetchData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error("Fetch failed:", error);
}
}
Optional Chaining
// Safely access nested properties
const city = user?.address?.city ?? "Unknown";
JavaScript for Agents: Common Patterns
Working with APIs
const response = await fetch("https://api.example.com/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({ query: "hello" })
});
const result = await response.json();
Data Transformation
const processed = data
.filter(item => item.active)
.map(item => ({ ...item, processed: true }))
.sort((a, b) => b.score - a.score)
.slice(0, 10);
Error Handling
try {
const result = await riskyOperation();
} catch (error) {
if (error instanceof NetworkError) {
await retry();
} else {
throw error;
}
}
TypeScript: JavaScript with Types
TypeScript adds static typing to JavaScript:
interface User {
id: string;
name: string;
email: string;
role: "admin" | "user";
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
Most modern AI agent codebases use TypeScript for better reliability.
The JavaScript Ecosystem
Frontend Frameworks
- React, Vue, Svelte, Angular
Backend Frameworks
- Express, Fastify, Hono, Koa
Full-Stack
- Next.js, Nuxt, SvelteKit
Testing
- Jest, Vitest, Playwright
Build Tools
- Vite, esbuild, webpack, Rollup
Quirks to Know
// JavaScript is... special
[] + [] // "" (empty string)
[] + {} // "[object Object]"
{} + [] // 0
NaN === NaN // false
typeof null // "object"
0.1 + 0.2 // 0.30000000000000004
These quirks are why TypeScript and strict linting are recommended.
See Also
JavaScript: The only language where typeof NaN === "number" is true. We don't question it anymore. 🤷