How does JavaScript's forEach loop decide to skip or iterate through "undefined" and "null&am

I know that forEach method will iterate through an array object and will skip all array elements which

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, the forEach loop ignores the 4th element a[3] which is undefined.
  • But, when fn(b) is executed, the forEach loop iterates through the 4th element b[3] which is also undefined.

What is the difference between a[3] and b[3] here? Why forEach loop didn't skip b[3]?

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, the forEach loop ignores the 4th element a[3] which is undefined.
  • But, when fn(b) is executed, the forEach loop iterates through the 4th element b[3] which is also undefined.

What is the difference between a[3] and b[3] here? Why forEach loop didn't skip b[3]?

Share Improve this question asked Jul 29, 2016 at 11:50 Aaditya SharmaAaditya Sharma 4,0103 gold badges27 silver badges38 bronze badges 3
  • 1 Possible duplicate of JavaScript 'in' operator for `undefined` elements in Arrays – JJJ Commented Jul 29, 2016 at 11:54
  • 3 The premise is just wrong: forEach won't skip elements that are null or undefined. It skips elements that don't exist at all. – JJJ Commented Jul 29, 2016 at 11:55
  • @JJJ is right. Try [null, , undefined].forEach(console.log). It prints null, skips the index with no value, then prints undefined. – Web_Designer Commented Jun 8, 2017 at 21:04
Add a ment  | 

2 Answers 2

Reset to default 2

According to the specifications of ECMAScript 5.1, missing array items are elided.

Whenever a ma in the element list is not preceded by an AssignmentExpression (i.e., a ma at the beginning or after another ma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined.

Since they are undefined, behaviour is unpredictable and there is no definition as to whether these should be treated as normal array items like undefined. I suspect that in some browsers, undefined may be returned, but this doesn't appear to be the case in Chrome. Here's what your first example of [1, 2, 3,, 5, 6] evaluates to:

[1, 2, 3, undefined × 1, 5, 6]

However, [1, 2, 3, undefined, 5, 6] just evaluates to:

[1, 2, 3, undefined, 5, 6]

As we can see, these are subtly different, so the behaviour is not the same even though they look superficially similar.

If you print the arrays in the console you will get:

var a = [1,2,3,,5,6];
var b = [1,2,3,undefined,5,6];
console.log(a);
console.log(b);

Console

    >[1, 2, 3, 4: 5, 5: 6]
    >
    0:1
    1:2
    2:3
    4:5
    5:6
    length:6


    >[1, 2, 3,undefined, 5,6]
    >
    0:1
    1:2
    2:3
    3:undefined
    4:5
    5:6
    length:6

So there are not element in index 3.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341837a4623361.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信