I realized that when I put a loop inside a function and want to return
the value it will give me only first value. Like in this example:
var arr = [[1,2], [3,4], [5,6]];
function getVal(item) {
var result;
for (var i = 0; i < item.length; i++) {
for (var j = 0; j < item[i].length; j++) {
return (item[i][j])
}
}
}
console.log(getVal(arr));
It will give me only the value of 1
. But when I put console.log
instead of return
in function it will give me expected result (1,2,3,4,5,6
). What's wrong here?