Typed arr ( type[] )
Try it yourself →An array containing elements of a specific type.
Syntax
let name: type[] = [...];
Description
number[] is an array that can only contain numbers, string[] only text.
Example
const scores: number[] = [90, 85, 100]; console.log(scores.map(p => p + 5));
Expected output
[ 95, 90, 105 ]
When to use?
Type-safe lists (scores, names).
Common mistakes
- ⚠️ const a: number[] = [1, "two"]; → ERROR: text cannot be in the array.