Lesson 6 of 9

Lesson 06 — Arrays and loops with discipline

Title: Iterate fixtures safely and avoid accidental mutation

Description: Combine typed arrays with for...of, classic indexes when needed, and immutability mindset for shared test data.

Why it matters for QA: Parallel specs share arrays — unintended mutation causes ghost failures only on CI.


1. for...of over roles

typescripttypescript
const users: string[] = ["admin", "guest", "editor"];

for (const user of users) {
  console.log(user.toUpperCase());
}

2. Need the index?

typescripttypescript
const ids = [101, 202, 303];

ids.forEach((id, index) => {
  console.log(index, id);
});

3. Typed tuples for coordinate-like data

An array = many values of the same type A tuple = fixed structure with known types and order

typescripttypescript
type Coordinate = [number, number];

const hotspots: Coordinate[] = [
  [120, 340],
  [44, 90],
];

4. Prefer copies before destructive ops

typescripttypescript
const priorities = ["P1", "P2", "P3"];
const sorted = [...priorities].sort();