ProgramGuru

Array Methods

Understand Array Methods

Built-in array methods transform, inspect, search, and combine collections. Choose a method whose behavior matches your intent.

What you will learn: the purpose of array methods, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how array methods works and where its type rules apply.
  • Predict the exact output of the array methods example.
  • Diagnose a common issue: A successful find can return a value, but unsuccessful find returns undefined; do not access its properties without checking.

Build the mental model

find returns the first matching element or undefined; includes checks membership; some and every answer boolean questions. Methods such as map and filter produce new arrays, while push and sort modify the existing array. Read the mutability contract of the method you choose.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Array Methods: explained visually[2,4,6,8] → slice(1,3) → [4,6][2,4,6,8]slice(1,3)[4,6]Follow the values through the operation
Array Methods: [2,4,6,8] → slice(1,3) → [4,6].

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

const items: number[] = [2, 4, 6, 8];
console.log(items.includes(6));
console.log(items.find(n => n > 5));
console.log(items.some(n => n % 2 !== 0));
console.log(items.slice(1, 3).join(", "));

Verified output

true
6
false
4, 6

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

includes(6) returns true. find returns the first value greater than 5, which is 6. some returns false because all are even. slice(1,3) makes [4,6] without changing items.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

A successful find can return a value, but unsuccessful find returns undefined; do not access its properties without checking.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Use filter to keep numbers at least 5. Use every to test whether they are all even.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Which method creates a portion of an array without mutating the source?
Choose one answer. You can try again.

Answer and explanation

slice.

Built-in array methods transform, inspect, search, and combine collections. Choose a method whose behavior matches your intent.

Review the key idea

Array Methods: Built-in array methods transform, inspect, search, and combine collections.

includes(6) returns true. find returns the first value greater than 5, which is 6. some returns false because all are even. slice(1,3) makes [4,6] without changing items.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: map(), filter(), and reduce() →