What does it mean to access an element in an array by index?
Accessing an element by index means retrieving the value stored at a specific position in the array, where the position is given by the index number starting from 0.
Click to reveal answer
beginner
How do you update an element at a specific index in an array?
You update an element by assigning a new value to the array at that index, for example: array[index] = newValue.
Click to reveal answer
beginner
What happens if you try to access an index outside the array length?
Accessing an index outside the array length returns undefined in JavaScript, meaning there is no value at that position.
Click to reveal answer
beginner
Explain the difference between accessing and updating an array element.
Accessing means reading the value at a given index without changing it. Updating means changing the value at that index to something new.
Click to reveal answer
beginner
In JavaScript, what is the index of the first element in an array?
The index of the first element in a JavaScript array is 0.
Click to reveal answer
What is the result of accessing array[2] if array = [10, 20, 30, 40]?
A30
B20
C40
Dundefined
✗ Incorrect
Index 2 corresponds to the third element, which is 30.
How do you update the first element of an array named 'nums' to 100?
Anums[0] = 100;
Bnums[1] = 100;
Cnums.update(0, 100);
Dnums.set(1, 100);
✗ Incorrect
The first element is at index 0, so nums[0] = 100 updates it.
What happens if you access array[5] when array length is 3?
AReturns last element
BReturns undefined
CThrows an error
DReturns null
✗ Incorrect
Accessing an index beyond the array length returns undefined in JavaScript.
Which index corresponds to the last element in an array of length 7?
A0
B7
C1
D6
✗ Incorrect
Array indices start at 0, so the last element is at length - 1, which is 6.
If you want to read the value at index 3 without changing it, what operation do you perform?
ADelete array[3]
BUpdate array[3]
CAccess array[3]
DInsert at array[3]
✗ Incorrect
Accessing means reading the value at that index without changing it.
Describe how to access and update an element at a specific index in an array.
Think about how you get and set values using the array name and index.
You got /3 concepts.
What happens if you try to access an index that is outside the current array length in JavaScript?
Consider what JavaScript returns when you ask for a position that does not exist.
You got /3 concepts.
Practice
(1/5)
1. What does arr[2] return if arr = [10, 20, 30, 40]?
easy
A. 40
B. 20
C. 10
D. 30
Solution
Step 1: Understand array indexing
Arrays start at index 0, so index 2 is the third element.
Step 2: Identify the element at index 2
The element at index 2 in [10, 20, 30, 40] is 30.
Final Answer:
30 -> Option D
Quick Check:
Index 2 element = 30 [OK]
Hint: Remember arrays start at zero index [OK]
Common Mistakes:
Thinking index starts at 1
Confusing element value with index
Using negative index without explanation
2. Which of the following is the correct way to update the 3rd element of array nums to 50?
easy
A. nums[3] = 50;
B. nums(3) = 50;
C. nums[2] = 50;
D. nums{2} = 50;
Solution
Step 1: Recall array indexing syntax
In JavaScript, arrays use square brackets with zero-based index.
Step 2: Identify correct index for 3rd element
The 3rd element is at index 2, so nums[2] = 50; is correct.
Final Answer:
nums[2] = 50; -> Option C
Quick Check:
Use square brackets and zero-based index [OK]
Hint: Use square brackets and zero-based index [OK]