// 创建一个数组
let fruits = ['苹果', '香蕉', '橙子'];
// 访问数组元素
console.log(fruits[0]); // 输出: 苹果
// 添加元素到数组末尾
fruits.push('葡萄');
console.log(fruits); // 输出: ["苹果", "香蕉", "橙子", "葡萄"]
// 删除数组最后一个元素
fruits.pop();
console.log(fruits); // 输出: ["苹果", "香蕉", "橙子"]
// 遍历数组
fruits.forEach(function(item, index) {
console.log(`第 ${index + 1} 个水果是 ${item}`);
});
// 查找数组中是否包含某个元素
let hasBanana = fruits.includes('香蕉');
console.log(hasBanana); // 输出: true
// 获取数组长度
console.log(fruits.length); // 输出: 3
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站