What if you could instantly find any day's data without flipping pages or searching endlessly?
Why Array Declaration and Initialization in DSA Typescript?
Imagine you want to keep track of your daily expenses for a month using just a notebook. You write each day's expense on a new line, but you have to flip pages back and forth to find or update a specific day's amount.
This manual method is slow and confusing. You might lose track of which line corresponds to which day, and updating or finding a specific day's expense takes too much time. Mistakes happen easily when the data grows.
Using arrays, you can store all daily expenses in a single, organized list where each position corresponds to a day. This makes it easy to access, update, or add expenses quickly without confusion.
let expenses = {};
expenses['day1'] = 20;
expenses['day2'] = 15;
// Hard to manage and accesslet expenses: number[] = [20, 15]; // Easy to access by index: expenses[0] for day 1
Arrays let you handle collections of data efficiently, making your programs faster and easier to manage.
Tracking daily temperatures for a week in a weather app, where each day's temperature is stored in an array for quick access and display.
Arrays organize multiple values in a single structure.
They allow quick access using positions (indexes).
Initialization sets up the array ready for use.
Practice
Solution
Step 1: Understand TypeScript array syntax
In TypeScript, arrays are declared using the type followed by square brackets, e.g., number[].Step 2: Check each option's syntax
const numbers: number[] = [1, 2, 3]; correctly declares an array of numbers. Options A, B, and D use incorrect syntax for arrays.Final Answer:
const numbers: number[] = [1, 2, 3]; -> Option AQuick Check:
Array declaration with type[] = correct syntax [OK]
- Using type without [] for arrays
- Using parentheses instead of square brackets
- Using curly braces instead of square brackets
Solution
Step 1: Identify correct type annotation for empty string array
To declare an empty array of strings, use string[] as the type and assign an empty array [].Step 2: Validate each option
let names: string[] = []; correctly uses let names: string[] = []. Others have syntax errors or wrong type placement.Final Answer:
let names: string[] = []; -> Option CQuick Check:
Empty array of strings = string[] = correct [OK]
- Omitting [] after type for arrays
- Assigning type incorrectly after =
- Using invalid syntax like let names = string[];
const arr: number[] = [10, 20, 30]; arr[1] = 50; console.log(arr);
Solution
Step 1: Understand array element assignment
Arrays allow changing elements by index. arr[1] changes the second element.Step 2: Apply the assignment and print
arr[1] = 50 changes the second element from 20 to 50. Printing arr shows [10, 50, 30].Final Answer:
[10, 50, 30] -> Option BQuick Check:
Index assignment updates array element [OK]
- Thinking arrays are immutable
- Confusing index 1 with index 0
- Expecting syntax error on assignment
let nums: number[] = (1, 2, 3);
Solution
Step 1: Check array initialization syntax
Arrays must be initialized with square brackets []. Parentheses () create a comma expression, not an array.Step 2: Identify the error
Using (1, 2, 3) does not create an array but evaluates to 3. This is incorrect for array initialization.Final Answer:
Using parentheses instead of square brackets for array initialization -> Option DQuick Check:
Arrays use [] not () for initialization [OK]
- Confusing parentheses with brackets
- Ignoring type annotations
- Thinking let vs const causes error here
Solution
Step 1: Understand 2D array declaration in TypeScript
2D arrays are arrays of arrays, so type is number[][].Step 2: Check initialization for 3 rows and 2 columns
const matrix: number[][] = [[0, 0], [0, 0], [0, 0]]; correctly initializes 3 arrays each with 2 zeros. const matrix: number[3][2] = [[0, 0], [0, 0], [0, 0]]; uses invalid syntax. const matrix: number[] = [0, 0, 0, 0, 0, 0]; is 1D array. const matrix = new Array(3, 2).fill(0); creates array with 2 elements, not 2D.Final Answer:
const matrix: number[][] = [[0, 0], [0, 0], [0, 0]]; -> Option AQuick Check:
2D array = array of arrays with type number[][] [OK]
- Using number[3][2] which is invalid in TypeScript
- Confusing 1D and 2D array initialization
- Using new Array with multiple arguments incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
