ProgramGuru

Basic TypeScript Programs

Basic TypeScript Programs — purpose and prerequisites

Basic TypeScript programs practice variables, types, branching, and formatted output. Each example should have a predictable input and result so beginners can focus on the language feature before adding prompts or file operations.

What you’ll learn

  • Explain the purpose of basic typescript programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The array values are added with reduce. Dividing by its length gives the average; toFixed(2) produces a display string with two decimal places.

Using an empty array without checking its length makes the average 0 / 0, which is NaN.

Visualize the flow

Basic TypeScript Programs — learning flowTyped variables → Array aggregation → Formatted reportBasic TypeScript Programs1Typed variables2Array aggregation3Formatted reportRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

const name: string = "Maya";
const marks: number[] = [78, 86, 92];
const total = marks.reduce((sum, mark) => sum + mark, 0);
const average = total / marks.length;
console.log(`${name}: ${total} marks`);
console.log(`Average: ${average.toFixed(2)}`);

Verified output

Maya: 256 marks
Average: 85.33

Compile: npx tsc --strict --target es2022 --module commonjs basic-programs.ts; run: node basic-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The array values are added with reduce. Dividing by its length gives the average; toFixed(2) produces a display string with two decimal places.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Using an empty array without checking its length makes the average 0 / 0, which is NaN.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Add a grade based on the average, then test the highest and lowest scores.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes basic typescript programs in the example?
Choose one answer. You can try again.

Answer and explanation

The array values are added with reduce.

The array values are added with reduce. Dividing by its length gives the average; toFixed(2) produces a display string with two decimal places.

Key idea to remember

The array values are added with reduce. Dividing by its length gives the average; toFixed(2) produces a display string with two decimal places.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Number Programs →