Predict Array push Output
Predict OutputBeginner
Question
What does this code print?
Code
const nums = [1, 2];
const newLen = nums.push(3);
console.log(newLen);
Hint
push returns the new length of the array after adding the element.
Explanation
The push method adds an element to the end of the array and returns the new length. The array starts with 2 elements, push(3) adds one more (now [1, 2, 3]), and returns the new length: 3.
Related Resources
Related Lessons
- JavaScript Arrays
Learn about JavaScript array methods like push.
Related Reference
- JavaScript Array Reference
Reference for JavaScript array methods.
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.