Frontend Basics — P3

· 1 min read · 231 Words · -Views -Comments

Will AI replace IT? Not anytime soon. Keep an eye on AI, but don’t forget the fundamentals — they’re the foundation, and AI is just a tool. Here’s a JS array question to illustrate some basics.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const list2 = [,undefined, null, '', 0, false, NaN, 1,];

for (let item of list) {
    console.log(item);
    list2.map(item => console.log(item));
}

console.log('list2.length', list2.length);

// 1、输出啥
// 2、list2长度是多少

Output

1
undefined
null

0
false
NaN
1
2
undefined
null

0
false
NaN
1
3
undefined
null

0
false
NaN
1
4
undefined
null

0
false
NaN
1
5
undefined
null

0
false
NaN
1
6
undefined
null

0
false
NaN
1
7
undefined
null

0
false
NaN
1
8
undefined
null

0
false
NaN
1
9
undefined
null

0
false
NaN
1
10
undefined
null

0
false
NaN
1

8

Key points

  1. A leading comma creates a “hole” at the head; trailing commas don’t add an extra hole.
  2. When iterating, holes don’t trigger the callback, but iteration still advances the index — the first printed index is 1, not 0.
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover