0
0
Javascriptprogramming~5 mins

Accessing array elements in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an array in JavaScript?
An array is a list that holds multiple values in order. You can think of it like a row of boxes, each with a number to find what’s inside.
Click to reveal answer
beginner
How do you access the first element of an array named fruits?
Use fruits[0]. Arrays start counting at 0, so the first box is number 0.
Click to reveal answer
beginner
What happens if you try to access an index that is outside the array length?
You get undefined because there is no value stored at that position.
Click to reveal answer
intermediate
How can you find the last element of an array arr without knowing its length?
Use arr[arr.length - 1]. arr.length gives the number of items, so subtract 1 to get the last index.
Click to reveal answer
intermediate
What is the difference between arr[1] and arr['1'] in JavaScript?
There is no difference. JavaScript converts the string '1' to the number 1 when accessing array elements.
Click to reveal answer
What index do arrays in JavaScript start counting from?
A1
BDepends on the array
C-1
D0
What will arr[5] return if arr has 3 elements?
Aundefined
BThe 6th element
CError
Dnull
How do you get the last element of an array myArray?
AmyArray[myArray.length - 1]
BmyArray[-1]
CmyArray[myArray.length]
DmyArray.last()
What does arr['2'] access in JavaScript?
AThe element at index '2' as a string key
BAn error because index must be a number
CThe element at index 2
DThe whole array
If colors = ['red', 'green', 'blue'], what is colors[1]?
A'red'
B'green'
C'blue'
Dundefined
Explain how to access elements in a JavaScript array and what happens if you use an index outside the array.
Think about how you find items in a list by their position.
You got /3 concepts.
    Describe how to get the last element of an array without knowing its length beforehand.
    Remember the array length counts items, but indexes start at zero.
    You got /3 concepts.