# 章序号/节序号/节/笔记序号 codecademy,array # 概念阐释 - 通过`.push()`内置对象来在**结尾**简单增加数组元素 # 举例子 ```js const itemTracker = ['item 0', 'item 1', 'item 2']; itemTracker.push('item 3', 'item 4'); console.log(itemTracker); // Output: ['item 0', 'item 1', 'item 2', 'item 3', 'item 4']; ``` - 之所以要先用push()再用console才能打印出添加的数组本身(内容),是因为JavaScript中的array.push方法会**返回添加后的数组的长度,而不是添加后的数组本身,这是array.push方法的设计特性** ```js const itemTracker = ['item 0', 'item 1', 'item 2']; console.log(itemTracker.push('item 3', 'item 4')); // Output: 5 ``` - [如果您想要返回添加后的数组本身,您可以使用array.concat方法来代替array.push方法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)[2](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)。 # 类比、比较与对比 # 问题 给这个变量增加2个新的不同数据类型的数组元素,并且print结果 ```js const chores = ['wash dishes', 'do laundry', 'take out trash']; ``` # 问题答案 ```js const chores = ['wash dishes', 'do laundry', 'take out trash']; chores.push(6,true); console.log(chores); ``` # 备注(经验集/错误集)