CodingNeed.

3 · Work with collections · 8 MIN

Keep an ordered list

Arrays store values in order, starting at index zero.

Use array[0] for the first item and array.length for the number of items. Reading a position that does not exist gives undefined. An empty list needs deliberate handling. Arrays are useful when you have several values of the same kind.

A row of numbered lockers starts at locker 0 in JavaScript.

Read the example

const names = ["Ada", "Lin", "Grace"];
console.log(names[0]);
console.log(names.length);
Check the expected output
Ada
3

Your challenge

Return the first item of the input array. Return null if the array is empty.

Common trap

The last index is length - 1, not length.

Next lesson: Repeat with a loop →

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.