I was doing a task in JavaScript and I notice when i try to print only half array the final array is being printed. Here the code:
let a = [];
for (let i = 1; i < 5; i++) {
if (i === 2) console.log(a)
a.push(i)
}
Here in output instead of printing array with only 1 element it is printing array with every element from 1 to 5. My question is why this is happening?