I know that forEach
method will iterate through an array object and will skip all array elements which are either null
or undefined
. I've an example below:
var a = [1,2,3,,5,6];
var b = [1,2,3,undefined,5,6];
var fn = function(arr){
arr.forEach(function(currentValue, index, array){
console.log(currentValue);
});
};
fn(a); //Prints on console (separated by newline): 1 2 3 5 6
fn(b); //Prints on console (separated by newline): 1 2 3 undefined 5 6
In above example,
- when
fn(a)
is executed, theforEach
loop ignores the 4th elementa[3]
which is undefined. - But, when
fn(b)
is executed, theforEach
loop iterates through the 4th elementb[3]
which is also undefined.
What is the difference between a[3]
and b[3]
here? Why forEach
loop didn't skip b[3]
?