liveBook · Manning

Array

As explained in Essential TypeScript 5, Third Edition

An array is an ordered collection data structure whose elements are accessed by position.

An array is an ordered collection whose elements are accessed by zero-based position. JavaScript arrays resize dynamically, accept mixed value types, and use square-bracket syntax. Create an empty array, then add values with push; read or replace an element with its index. section 3.4

A practical JavaScript array

The names array starts with three strings, while prices grows as values are appended. The example deliberately mixes numbers and strings, then converts values before calculating a numeric total. It prints Hat as the first item and a total of 250.25.

TypeScript preserves JavaScript’s flexible length but can restrict element types. Write number[] or string[] annotations, and use (number | string)[] when a union is intentional. Typed arrays let callbacks such as forEach receive values with the declared type, enabling operations such as toFixed only on numbers. section 9.2

Common array operations include:

  • Arrays of objects inherit their element type from object shape; mixing shapes can leave only common properties safely available. section 10.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains creation, indexing, resizing, mutation methods, typed callbacks, unions, and object shapes, but does not discuss implementation internals.
Examples
Uses names and prices, typed product prices, forEach tax calculation, and mixed-shape product objects to expose ordinary and erroneous cases.
What it assumes
Assumes basic JavaScript familiarity while using arrays to clarify what TypeScript adds through annotations and compiler checks.
What you can do after
Apply it via JavaScript and TypeScript syntax to create, index, mutate, iterate, type, and diagnose arrays in application code.
How it connects
Connects arrays with JavaScript, TypeScript, type annotations, union types, forEach, object shapes, and type inference.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.