Predict Array State Update
Predict Output Medium
Question
What is the length of `items` after this update?
Code
const [items, setItems] = useState([1, 2, 3]);
setItems([...items, 4]);
// What is items.length?
Hint
The spread copies existing items, then 4 is appended.
Explanation
The spread [...items, 4] copies [1, 2, 3] and appends 4, resulting in [1, 2, 3, 4]. The length is 4.
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.