An array is a list-like object used to store multiple values in a single variable, where each value has an index starting from 0.
Click to reveal answer
beginner
How do you declare an empty array in JavaScript?
You can declare an empty array using square brackets: <code>const arr = [];</code>
Click to reveal answer
beginner
Show how to declare and initialize an array with numbers 1, 2, and 3.
Use: <code>const arr = [1, 2, 3];</code> This creates an array with three elements: 1 at index 0, 2 at index 1, and 3 at index 2.
Click to reveal answer
intermediate
What happens if you declare an array with a size but no elements, like <code>const arr = new Array(3);</code>?
It creates an array with length 3 but all elements are empty slots. You need to assign values later.
Click to reveal answer
beginner
Can JavaScript arrays hold different types of values together? Give an example.
Yes, arrays can hold mixed types. Example: <code>const arr = [1, 'hello', true];</code> stores a number, a string, and a boolean.
Click to reveal answer
How do you declare an empty array in JavaScript?
Aconst arr = [];
Bconst arr = ();
Cconst arr = {};
Dconst arr = new Array[];
✗ Incorrect
Square brackets [] are used to declare arrays. Parentheses () and curly braces {} are for other data types.
What is the index of the first element in a JavaScript array?
A0
B1
C-1
DDepends on the array
✗ Incorrect
JavaScript arrays start indexing from 0, so the first element is at index 0.
What does new Array(5) create?
AAn array with 5 undefined elements
BAn array with 5 empty slots
CAn array with 5 zeros
DAn error
✗ Incorrect
It creates an array of length 5 with empty slots, not actual undefined values.
Which of these is a valid array initialization with mixed types?
Aconst arr = new Array(1, 'two', true);
Bconst arr = (1, 'two', true);
Cconst arr = {1, 'two', true};
Dconst arr = [1, 'two', true];
✗ Incorrect
Square brackets [] create arrays and can hold mixed types.
How do you access the third element of an array named arr?
Aarr.get(3)
Barr[3]
Carr[2]
Darr(3)
✗ Incorrect
Array indexing starts at 0, so the third element is at index 2.
Explain how to declare and initialize an array with three elements in JavaScript.
Think about how you write a list of items inside square brackets.
You got /4 concepts.
Describe the difference between [] and new Array(3) when declaring arrays.
Consider the length and content of arrays created by each.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the correct way to declare an empty array in JavaScript?
easy
A. const arr = ();
B. const arr = [];
C. const arr = {};
D. const arr = array();
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 B
Quick Check:
Array declaration uses [] = const arr = []; [OK]
Hint: Arrays use square brackets [] for declaration [OK]
Common Mistakes:
Using parentheses () instead of square brackets
Using curly braces {} which declare objects
Trying to call array() like a function
2. Which of the following is the correct syntax to declare and initialize an array with values 1, 2, and 3?
easy
A. const arr = array[1, 2, 3];
B. const arr = (1, 2, 3);
C. const arr = {1, 2, 3};
D. const arr = [1, 2, 3];
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 D
Quick Check:
Array with values uses [] and commas = const arr = [1, 2, 3]; [OK]
Hint: Use square brackets with commas to list values [OK]
Common Mistakes:
Using parentheses instead of square brackets
Using curly braces which create objects
Trying to use 'array' keyword which doesn't exist
3. What will be the output of the following code?
const arr = new Array(3); console.log(arr);
medium
A. [undefined, undefined, undefined]
B. [0, 0, 0]
C. [empty x 3]
D. SyntaxError
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 C
Quick Check:
new Array(3) creates sparse array = [empty x 3] [OK]
Hint: new Array(n) creates empty slots, not undefined values [OK]
Common Mistakes:
Assuming new Array(n) fills with zeros
Thinking undefined values appear explicitly
Expecting a syntax error
4. Identify the error in the following array declaration and fix it:
const arr = new Array[1, 2, 3];
medium
A. Replace square brackets with parentheses: new Array(1, 2, 3);
B. Remove 'new' keyword: Array[1, 2, 3];
C. Use curly braces instead: new Array{1, 2, 3};
D. No error, code is correct
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 A
Quick Check:
Array constructor uses () not [] = Replace square brackets with parentheses: new Array(1, 2, 3); [OK]
Hint: Array constructor needs parentheses, not square brackets [OK]
Common Mistakes:
Using square brackets with new Array
Removing 'new' keyword incorrectly
Using curly braces which are invalid here
5. How can you programmatically create a 2D array (array of arrays) with 3 rows and 2 columns, all initialized to zero in JavaScript?
hard
A. const arr = Array.from({length: 3}, () => Array(2).fill(0));
B. const arr = Array(3).fill(Array(2).fill(0));
C. const arr = [[0,0],[0,0],[0,0]];
D. All of the above
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.