📖 Scenario: Imagine you are managing a list of favorite fruits that should not be changed accidentally in your program. You want to make sure this list stays the same throughout your code.
🎯 Goal: You will create a read-only array of fruits, then try to work with it safely without changing the original list.
📋 What You'll Learn
Create a read-only array of strings with exact fruits
Create a variable to hold the number of fruits
Use a for loop to list all fruits
Print the list of fruits
💡 Why This Matters
🌍 Real World
Read-only arrays help protect important lists of data from accidental changes, like a fixed menu or a list of constants.
💼 Career
Understanding read-only arrays is useful for writing safer TypeScript code in jobs that require data integrity and immutability.
Progress0 / 4 steps
1
Create a read-only array of fruits
Create a read-only array called fruits with these exact values: 'apple', 'banana', 'cherry', 'date'.
Typescript
Hint
Use readonly string[] to make the array read-only.
2
Create a variable for the number of fruits
Create a variable called count and set it to the length of the fruits array.
Typescript
Hint
Use the length property of the array.
3
Use a for loop to list all fruits
Use a for loop with variable i from 0 to count - 1 to access each fruit in fruits.
Typescript
Hint
Use a classic for loop to go through the array by index.
4
Print the list of fruits
Print the entire fruits array using console.log.
Typescript
Hint
Use console.log(fruits) to print the whole array.
Practice
(1/5)
1. What does a readonly array in TypeScript do?
easy
A. Only prevents changing the array length
B. Allows adding new elements but not removing existing ones
C. Makes the array elements optional
D. Prevents any changes to the array elements after creation
Solution
Step 1: Understand the readonly keyword
The readonly keyword before an array type means the array cannot be changed after it is created.
Step 2: Effects on the array
This means you cannot add, remove, or modify elements in the array once it is set.
Final Answer:
Prevents any changes to the array elements after creation -> Option D
Quick Check:
readonly arrays are immutable [OK]
Hint: Readonly arrays block all changes to elements [OK]
Common Mistakes:
Thinking readonly only blocks length changes
Assuming elements can be modified individually
Confusing readonly with optional elements
2. Which of the following is the correct syntax to declare a read-only array of numbers in TypeScript?
easy
A. let nums: readonly number[] = [1, 2, 3];
B. let nums: number[] readonly = [1, 2, 3];
C. let nums: number readonly[] = [1, 2, 3];
D. let nums: number[] = readonly [1, 2, 3];
Solution
Step 1: Recall the correct readonly syntax
In TypeScript, readonly comes before the array type, like readonly number[].
Step 2: Check each option
let nums: readonly number[] = [1, 2, 3]; uses readonly number[], which is correct syntax. Others place readonly incorrectly.
Final Answer:
let nums: readonly number[] = [1, 2, 3]; -> Option A
Quick Check:
readonly before type [OK]
Hint: Put readonly before the array type, not after [OK]
Common Mistakes:
Placing readonly after the array brackets
Using readonly as a variable modifier
Trying to put readonly inside the array literal
3. What will be the output of this TypeScript code?
A. Type 'number' is not assignable to type 'readonly number[]'
B. Cannot assign to '0' because it is a read-only property
C. Syntax error: missing semicolon
D. No error, code runs fine
Solution
Step 1: Understand the readonly array behavior
The array nums is readonly, so its elements cannot be changed after creation.
Step 2: Analyze the assignment to an element
Trying to assign nums[0] = 10 causes an error because elements are read-only.
Final Answer:
Cannot assign to '0' because it is a read-only property -> Option B
Quick Check:
Readonly elements cannot be changed [OK]
Hint: Readonly arrays block element assignment [OK]
Common Mistakes:
Thinking only array structure is readonly, not elements
Ignoring TypeScript error messages
Assuming semicolon missing causes error
5. Given a function that accepts a read-only array, which of these operations inside the function is allowed?
function process(items: readonly number[]) {
// Which line is valid?
// A) items.push(5);
// B) items[0] = 10;
// C) const first = items[0];
// D) items.length = 0;
}
hard
A. const first = items[0];
B. items.push(5);
C. items[0] = 10;
D. items.length = 0;
Solution
Step 1: Understand read-only array restrictions
Read-only arrays do not allow modifying methods like push, element assignment, or length changes.
Step 2: Check each operation
Only reading elements, like const first = items[0];, is allowed. Others cause errors.
Final Answer:
const first = items[0]; -> Option A
Quick Check:
Reading elements allowed, modifying disallowed [OK]
Hint: Readonly arrays allow reading, not modifying [OK]
Common Mistakes:
Trying to push or assign elements in readonly arrays
Attempting to change array length
Confusing read-only with mutable arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.