Question:

Which of the following JavaScript statements can be used to access the last element of an array named myArray?

Show Hint

In modern JavaScript, you can also use the newer .at() method to get the last element: myArray.at(-1) works just like myArray[myArray.length - 1].
Updated On: Jun 29, 2026
  • myArray [myArray.length+1]
  • myArray [myArray.length]
  • myArray [myArray.length - 1]
  • myArray [MyArray]
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation



Step 1: Understanding Array Indexing:

JavaScript arrays use zero-based indexing. This means the first element of an array is at index 0, the second is at index 1, and so on.

Step 2: Linking Array Length to Last Index:

If an array contains $N$ elements, its .length property returns $N$. Because indexing starts at $0$, the indices of the array range from: $$0 \text{ to } (N - 1)$$ Therefore, the last element is always located at index: $$\text{Last Index} = \text{myArray.length} - 1$$

Step 3: Verification of Options:

Assume an array myArray = ["A", "B", "C"] with a length of $3$.
(A) myArray[length+1]: Tries to access index $4$, which is empty/undefined.
(B) myArray[length]: Tries to access index $3$, which is also undefined (one index past the last valid element).
(C) myArray[length-1]: Accesses index $2$ ("C"), successfully retrieving the last element.
(D) myArray[MyArray]: Passes an object reference as an index, which is invalid.
Was this answer helpful?
0
0