// Create const fruits = ["apple", "banana", "mango"] const numbers = [1, 2, 3, 4, 5] const mixed = [1, "hello", true, null] // mixed types OK // Access by index (0-based) fruits[0] // "apple" fruits[1] // "banana" fruits[2] // "mango" // Length fruits.length // 3 // Modify fruits[0] = "pineapple" // replace fruits[3] = "orange" // add at index 3 // Last element fruits[fruits.length - 1] // "orange" fruits.at(-1) // "orange" (modern)
let arr = [1, 2, 3] // Add to end arr.push(4) // [1,2,3,4] arr.push(5, 6) // [1,2,3,4,5,6] // Remove from end arr.pop() // removes 6, returns 6 // Add to front arr.unshift(0) // [0,1,2,3,4,5] // Remove from front arr.shift() // removes 0, returns 0 // Remove/insert anywhere arr.splice(2, 1) // remove 1 element at index 2 arr.splice(2, 0, 99) // insert 99 at index 2
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // map — transform each element → new array const doubled = numbers.map(n => n * 2) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] // filter — keep elements where condition=true → new array const evens = numbers.filter(n => n % 2 === 0) // [2, 4, 6, 8, 10] // reduce — combine all into one value const sum = numbers.reduce((acc, n) => acc + n, 0) // 55 // Chaining! const result = numbers .filter(n => n % 2 === 0) // evens: [2,4,6,8,10] .map(n => n * n) // squared: [4,16,36,64,100] .reduce((acc, n) => acc + n, 0) // sum: 220
const nums = [3, 7, 2, 9, 4, 1, 8] // find — first match or undefined nums.find(n => n > 5) // 7 // findIndex — index of first match nums.findIndex(n => n > 5) // 1 // some — at least one match? (boolean) nums.some(n => n > 8) // true (9 exists) // every — all match? (boolean) nums.every(n => n > 0) // true // includes — value exists? (boolean) nums.includes(7) // true nums.includes(99) // false
let arr = [3, 1, 4, 1, 5, 9, 2, 6]
// sort (alphabetically by default!)
arr.sort() // [1, 1, 2, 3, 4, 5, 6, 9]
// sort numbers correctly
arr.sort((a, b) => a - b) // ascending
arr.sort((a, b) => b - a) // descending
// reverse
arr.reverse()
// join — array to string
["a", "b", "c"].join(", ") // "a, b, c"
["a", "b", "c"].join("") // "abc"
// flat — flatten nested array
[1, [2, 3], [4, [5]]].flat() // [1,2,3,4,[5]]
[1, [2, 3], [4, [5]]].flat(2) // [1,2,3,4,5]
// spread — copy array
const copy = [...arr]
const combined = [...arr1, ...arr2]
| Method | ဘာလုပ်တယ် | Return |
|---|---|---|
push(x) | End မှာ ထည့် | new length |
pop() | End ကနေ ဖယ် | removed item |
unshift(x) | Front မှာ ထည့် | new length |
shift() | Front ကနေ ဖယ် | removed item |
map(fn) | Transform each | new array |
filter(fn) | Keep matching | new array |
reduce(fn,init) | Combine all | single value |
find(fn) | First match | item or undefined |
some(fn) | Any match? | boolean |
every(fn) | All match? | boolean |
includes(x) | Contains? | boolean |
sort(fn) | Sort (mutates!) | same array |
join(sep) | Array → String | string |
slice(s,e) | Extract portion | new array |
concat(arr) | Combine arrays | new array |
← JS 04 | Next: JS Lesson 06 → Objects →