What if you could instantly find any item in your messy list without searching forever?
Why Array Declaration and Initialization in DSA Javascript?
Imagine you have a list of your favorite movies written on separate pieces of paper scattered on your desk. You want to keep them organized so you can quickly find or add a movie.
Keeping those papers loose means you spend a lot of time searching, losing some, or mixing up the order. It's slow and frustrating to manage your list manually.
An array is like a neat row of labeled boxes where you can place each movie. You can easily add, find, or change movies by looking at the box number, making your list organized and quick to use.
let movie1 = 'Inception'; let movie2 = 'Matrix'; let movie3 = 'Interstellar';
let movies = ['Inception', 'Matrix', 'Interstellar'];
Arrays let you store many items together in order, so you can quickly access or update any item by its position.
Think of a music playlist on your phone where songs are stored in order. You can play, skip, or add songs easily because they are kept in an array-like list.
Arrays organize multiple items in a single structure.
They allow quick access by position (index).
They make managing lists easier and less error-prone.
Practice
Solution
Step 1: Understand array declaration syntax
In JavaScript, arrays are declared using square brackets [].Step 2: Check each option
const arr = []; uses square brackets which is correct. Options B and C use parentheses and curly braces which are incorrect for arrays. const arr = array(); uses a function call which is not valid.Final Answer:
const arr = []; -> Option BQuick Check:
Array declaration uses [] = const arr = []; [OK]
- Using parentheses () instead of square brackets
- Using curly braces {} which declare objects
- Trying to call array() like a function
Solution
Step 1: Recall array initialization syntax
Arrays are initialized with square brackets containing comma-separated values.Step 2: Evaluate each option
const arr = [1, 2, 3]; correctly uses square brackets with values. Options B and C use parentheses and curly braces which are invalid. const arr = array[1, 2, 3]; uses incorrect syntax with 'array' keyword.Final Answer:
const arr = [1, 2, 3]; -> Option DQuick Check:
Array with values uses [] and commas = const arr = [1, 2, 3]; [OK]
- Using parentheses instead of square brackets
- Using curly braces which create objects
- Trying to use 'array' keyword which doesn't exist
const arr = new Array(3);
console.log(arr);
Solution
Step 1: Understand new Array(n) behavior
Using new Array(3) creates an array with length 3 but no defined values, called a sparse array.Step 2: Check console output for sparse arrays
Console shows sparse arrays as [empty x 3], not as undefined or zeros.Final Answer:
[empty x 3] -> Option CQuick Check:
new Array(3) creates sparse array = [empty x 3] [OK]
- Assuming new Array(n) fills with zeros
- Thinking undefined values appear explicitly
- Expecting a syntax error
const arr = new Array[1, 2, 3];
Solution
Step 1: Identify correct syntax for new Array()
The Array constructor requires parentheses, not square brackets, to pass elements.Step 2: Fix the syntax
Change new Array[1, 2, 3] to new Array(1, 2, 3) to correctly create the array.Final Answer:
Replace square brackets with parentheses: new Array(1, 2, 3); -> Option AQuick Check:
Array constructor uses () not [] = Replace square brackets with parentheses: new Array(1, 2, 3); [OK]
- Using square brackets with new Array
- Removing 'new' keyword incorrectly
- Using curly braces which are invalid here
Solution
Step 1: Understand 2D array initialization
Using Array.from with a function creates separate inner arrays to avoid shared references.Step 2: Analyze each option
const arr = Array(3).fill(Array(2).fill(0)); fills outer array with the same inner array reference, causing changes in one row to affect all. const arr = Array.from({length: 3}, () => Array(2).fill(0)); correctly creates separate inner arrays. const arr = [[0,0],[0,0],[0,0]]; manually creates the array but is less flexible. All of the above is incorrect because only A is fully correct for dynamic initialization.Final Answer:
const arr = Array.from({length: 3}, () => Array(2).fill(0)); -> Option AQuick Check:
Use Array.from with function for separate inner arrays = const arr = Array.from({length: 3}, () => Array(2).fill(0)); [OK]
- Using fill with array causes shared references
- Manually typing arrays is inflexible
- Assuming all options are correct
Ready to prove it?
Pass the test and this pattern moves unknown
