|
| 1 | +// arrays |
| 2 | + |
| 3 | +const fruits = ["apple", "banana", "cherry"]; |
| 4 | +const numbers = new Array(1, 2, 3); |
| 5 | +//empty array |
| 6 | +const emptyArray = new Array(); |
| 7 | +const lengthArray = new Array(5); // Creates an empty array of length 5 |
| 8 | + |
| 9 | +console.log(fruits[0]); // Outputs: 'apple' |
| 10 | +console.log(fruits[2]); // Outputs: 'm |
| 11 | + |
| 12 | +const moreFruits = ["pineapple", "berry"]; |
| 13 | +const allFruits = fruits.concat(moreFruits); |
| 14 | +console.log(allFruits); // Outputs: ['kiwi', 'orange', 'mango', 'pineapple', 'berry'] |
| 15 | + |
| 16 | +fruits.forEach((fruit) => { |
| 17 | + console.log(fruit); |
| 18 | +}); |
| 19 | +// Outputs: 'kiwi', 'lemon', 'orange', 'mango' |
| 20 | + |
| 21 | +const shortFruits = fruits.filter((fruit) => fruit.length < 6); |
| 22 | +console.log(shortFruits); // Outputs: ['kiwi', 'lemon', 'mango'] |
| 23 | + |
| 24 | +const found = fruits.find((fruit) => fruit.startsWith("m")); |
| 25 | +console.log(found); // Outputs: 'mango' |
| 26 | + |
| 27 | +const numbers2 = [1, 2, 3, 4]; |
| 28 | +const sum = numbers2.reduce((total, num) => total + num, 0); |
| 29 | +console.log(sum); // Outputs: 10 |
| 30 | + |
| 31 | +fruits.sort(); |
| 32 | +console.log(fruits); // Outputs: ['kiwi', 'lemon', 'mango', 'orange'] |
| 33 | + |
| 34 | +const numbers3 = [40, 100, 1, 5]; |
| 35 | +numbers3.sort((a, b) => a - b); |
| 36 | +console.log(numbers3); // Outputs: [1, 5, 40, 100] |
| 37 | + |
| 38 | + |
0 commit comments