Interviewer: What does func() print in this code?
const arr = [1, 2, 3, 4];
for (let i = 0; i < arr.length; i++) {
func(arr[i]);
arr.splice(0, 1);
}
function func(item) {
console.log(item);
}
A huge grin appeared on Jimmyâs face.
After all the many rounds of interviews heâd finally gotten to this last question, and it was easier than he ever thought it would be.
Get this right and the job was assured.
âIt prints 1, 2, 3, and 4â, he confidently proclaimed.
Unfortunately this turned out to be a huge mistake, and the interview was ended immediately.
âBut how?! What else would it print? I got it correctly!â, He fumed at the interviewer.
âNo, you didnât. Your foundation of JavaScript is clearly not solid enoughâ.
âOkay okay wait, I get it now â splice() mutates the array on every iteration right?
I see it now! I forgot that element indexes change when you remove elements.
Let me work this out:â
iter 1: [1, 2, 3, 4], index: 0, print: 1
iter 2: [2, 3, 4], index: 1, print: 3
iter 3: [3, 4], index: 2, print: undefined
iter 4: [4], index: 3, print: undefined
âOh so it really prints 1, 3, undefined, and undefinedâ. He looked up in desperate reassurance.
It was the interviewerâs turn to grin.
âWrong againâ.
Alright, enough of this nonsense.
âWhat the hell is wrong with you?â, He boomed as he shot up from his seat.
âIâve worked my ass off for your stupid company and I havenât even gotten the job!
And now youâre giving me this simple question for what? To insult my intelligence? Test my confidence? You think I donât how splice() works? Or for loops, like what do you take me for?â
âYou think I give a damn about your useless organization? You think I gave up my $200K job at Google for this crapâŠâ, the ranting and raving went on and onâŠ
The interviewerâs smile never wavered as the candidate continued his tirade, his words increasingly filled with frustration. He stood, clenching and unclenching his fists, the tension palpable.
âI donât need this! Iâve mastered JavaScript, built complex applications, and solved problems you couldnât even comprehend. And now this? A trick question? Who even writes code like that in production? This is absurd!â
The interviewer calmly leaned back in his chair and crossed his arms, still maintaining that infuriating, knowing smile.
âAre you finished?â, the interviewer asked after a long pause, the candidate breathing heavily.
Jimmy glared at him, his heart racing, his fists clenched, ready for another volley. But the interviewerâs calm demeanor somehow doused the fire in his chest.
âMy friend, the issue is not with splice() or how the loop behaves.
The core of the problem lies in how youâre thinking about the arrayâs length in the for loopâ
âWhat the hell are you talking about?â he sat back down.
âLet me show youâ, he came closer to Jimmyâs laptop.
const arr = [1, 2, 3, 4];
for (let i = 0; i < arr.length; i++) {
console.log('here');
arr.splice(0, 1);
}
âDo you see how many times the loop ran?â

âWait⊠wha⊠how are there only 2 iterations?â, Jimmy couldnât believe his eyes.
âYou see what I said about you lacking a fundamental understanding of JavaScript? You are clueless about for loopsâ
âDonât you realize that the length property is recalculated every time the loop runs?â
Jimmyâs face flushed as the realization sank in.
âSo I hope you can see what really happens in the code:â
const arr = [1, 2, 3, 4];
for (let i = 0; i < arr.length; i++) {
func(arr[i]);
arr.splice(0, 1);
}
function func(item) {
console.log(item);
}
It prints just 1 and 3:

âPeople like you are so used to looping to a constant number. You didnât even consider the possibility that arr.length could change, like here again:â
let n = 10;
for (let i = 0; i < n; i++) {
console.log(n--);
}
Jimmy nodded slowly, his anger replaced by quiet contemplation.
âI should have seen itâ.
âWe all have rough moments. Take this as a chance to reflect and improve.â
Jimmy shook his hand, humbled. This interview was still over so he rose up to leave.
âAlright, so um⊠do I still get the job?â
The interviewer burst into a hysterical feat of laughter.
âDonât push it my manâ.
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
