Programming Foundations for LLM Applications

Core JavaScript


Learning Objectives

  • You know the JavaScript language features used most often in the course.
  • You can work with arrays, objects, and functions in small CLI programs.
  • You can read and write simple module-based JavaScript code.

Values, variables, and functions

In this course, most programs are small data-processing programs. This means that we mostly work with strings, numbers, arrays, objects, and functions.

A simple function in JavaScript can be written like this:

const countWords = (text) => {
  return text.split(/\s+/).filter((word) => word.length > 0).length;
};

The function takes a string, transforms it, and returns a number. This kind of small function is a recurring building block throughout the course.

It is useful to read a function like this as a short pipeline. First the text is split into pieces. Then empty pieces are removed. Finally the length of the remaining array is returned. Many functions in this course follow exactly that pattern: take an input value, perform a few explicit transformations, and return a result that later code can use.

If you already know another programming language, this style will probably feel familiar. The main difference is often only the syntax.

Arrays and objects

Arrays are useful when the program handles many values of the same kind. Objects are useful when values belong together.

const documents = [
  { filename: "hello.txt", totalWords: 2 },
  { filename: "notes.txt", totalWords: 48 },
];

Here, documents is an array and each document entry is an object. This structure is common in CLI programs because it is easy to inspect, process, and later serialize into JSON.

This distinction matters because many programming mistakes come from mixing the two ideas. If the program needs “many document records”, an array of objects is usually the right fit. If it needs “one report with several fields”, a single object is often clearer. Keeping these roles separate makes later code easier to read and easier to test.

Transforming arrays

Three array operations appear frequently in the course: map, filter, and reduce:

  • map creates a new array by transforming each item,
  • filter keeps only the items that satisfy a condition, and
  • reduce combines many values into one summary value.

In practice, these often appear together in exactly the kind of small CLI pipelines that this course uses.

Suppose that a program reads several document records and wants only the filenames of non-empty documents:

const documents = [
  { filename: "hello.txt", totalWords: 2 },
  { filename: "notes.txt", totalWords: 48 },
  { filename: "empty.txt", totalWords: 0 },
];

const nonEmptyFilenames = documents
  .filter((document) => document.totalWords > 0)
  .map((document) => document.filename);

The code first removes the unwanted records and only then extracts the filenames. This left-to-right style is common in the course because it makes the data flow visible.

const wordCounts = [2, 48, 13];
const totalWords = wordCounts.reduce((sum, count) => sum + count, 0);

When you see a program that produces a summary from many input values, there is a good chance that map, filter, or reduce is involved somewhere. It is worth becoming comfortable with them early, because they appear repeatedly in parsing, reporting, retrieval, and evaluation code later in the course.

Loading Exercise...

The same left-to-right idea also appears in small command-line programs that read data, transform it, and print a result.

Loading Exercise...

Destructuring

Destructuring is a compact way to retrieve values from objects or arrays.

const report = {
  totalDocuments: 2,
  totalWords: 50,
};

const { totalDocuments, totalWords } = report;

Destructuring is convenient when a function returns a structured result and only some parts are needed immediately. It is also a signal to the reader: these are the fields that matter in the next few lines.

You may also see destructuring in function parameters:

const printReportHeader = ({ totalDocuments, totalWords }) => {
  console.log(`Documents: ${totalDocuments}`);
  console.log(`Total words: ${totalWords}`);
};

This can make small helper functions clearer, but it is still worth keeping the structure simple. If a function needs many unrelated fields, that is often a sign that the design should be reconsidered.

Modules

JavaScript modules allow splitting a program into smaller files. In this course, we usually export named functions from one file and import them in another.

// analyzer.js
const analyzeText = (text) => {
  return { totalWords: countWords(text) };
};

export { analyzeText };
// main.js
import { analyzeText } from "./analyzer.js";

This is enough to support small but clear command-line projects. The main point is not that every project should be split into many files. The point is that once a program starts mixing reading input, processing data, and formatting output in the same place, modules make it easier to separate those responsibilities.

In the tutorial chapter of this part, we will use exactly this style. One file will read files, another will analyze text, and a third will format the result. The syntax is simple; the real benefit is conceptual separation.

Loading Exercise...

Keep the language scope narrow

You do not need advanced JavaScript features to follow this course. A clear command of basic values, arrays, objects, functions, and modules is much more useful than wide but shallow syntax coverage.