A for..in loop in JavaScript loops over the enumerable properties of an object. Here, you are trying to use for..in on arr, which is not an array, it's a NodeList (as that's the return value of querySelectorAll).
A NodeList has the enumerable properties of:
0
1
2
length
item
entries
forEach
keys
values
As you can see, it doesn't just contain the indexes of arr, but rather all the enumerable properties. This means your for..in loop will go through each of these. That's why you get undefined in your array as arr['length'].value for example will give you undefined.
For looping a NodeList, you can use a regular for loop, a for..of loop or NodeList.prototype.forEach():
const test = document.getElementById('test');
const nodes = test.querySelectorAll('input');
const arr2 = [];
for (const input of nodes) {
arr2.push(input.value);
}
console.log(arr2);
<div id='test'>
<input name="M1" size="3" value="1">
<input name="M2" size="3" value="2">
<input name="M3" size="3" value="3">
</div>
As a side note, if you're trying to build an array of values from your inputs, you can use Array.from() with a mapping function on your NodeList like so:
const test = document.getElementById('test');
const nodes = test.querySelectorAll('input');
const arr2 = Array.from(nodes, ({value}) => value);
console.log(arr2);
<div id='test'>
<input name="M1" size="3" value="1">
<input name="M2" size="3" value="2">
<input name="M3" size="3" value="3">
</div>
for (i in arr) { console.log(i); arr2.push( arr[i].value ); }I suspect that this may make it clearer. Incidentally:let arr2 = Array.from( test.querySelectorAll('input') ).map( (inputEl) => inputEl.value )would be a less complex way of achieving your (apparent) desired end result. Also: see thefor...indocumentation on MDN.for (const i of arr) { arr2.push(i.value) }instead,inis not a collection iterator, butofis..