JavaScript Arrays Quiz
Arrays are some of the most used structures in JavaScript, and some of the most surprising. sort() compares items as strings by default, map() passes more arguments to its callback than you might think, and writing past the end of an array leaves empty slots behind.
Each question below shows a short snippet. Work out the output, then read the explanation to see exactly what the array methods do under the hood.
Arrays questions
- Why does [33, 2, 8].sort() not sort numbers correctly?
- What happens when you assign past the end of a JavaScript array?
- Why does [1, 2, 3].map(parseInt) return [1, NaN, NaN]?
- What does Array.push() return in JavaScript?
- How do you remove duplicates from an array with Set?
- What does Array.from({ length: 3 }) create?
- Why is [1, 2, 3] == "1,2,3" true?
- Why does indexOf not find NaN but includes does?
- What happens when you set an array's length?
- Why does reduce throw on an empty array?
- Can you push to a const array in JavaScript?