
Our Javascript Code:
Step-by-Step Breakdown
Step 1: Understand .push()
The Array.prototype.push()
method:
- Adds one or more elements to the end of an array.
- Returns the new length of the array not the updated array itself.
Example :-
Step 2: Apply It to Your Code
Here’s what’s really happening:
[1]
is your original array..push(2)
adds the number 2
to that array, so it becomes [1, 2]
.- But
.push()
returns 2
(the new length of the array), not the array itself.
So now:
Step 3: Second Line
You're trying to call .push()
on newList
, but:
And 2
is a number, not an array.
So you get:
Because numbers don’t have a .push()
method.
✔ Final Answer:
D) TypeError
This question appeared in the Capgemini 2nd round of interview, testing the candidate’s knowledge of array methods in JavaScript.