โž•โž– Adding & Removing Items

push()

  • Add to the end
  • Use for: adding to cart, appending logs
  • Returns: the new array length
const cart = ['apple'];
cart.push('banana'); // 2

pop()

  • Remove last item
  • Use for: undo last action, stack operations
  • Returns: the removed item
const stack = ['init', 'save', 'close'];
stack.pop(); // 'close'

shift()

  • Remove first item
  • Use for: processing queues, removing oldest entry
  • Returns: the removed item
const queue = ['first', 'second', 'third'];
queue.shift(); // 'first'

unshift()

  • Add to the beginning
  • Use for: prepending recent items, priority queues
  • Returns: the new array length
const updates = ['old'];
updates.unshift('new'); // 2

splice()

  • Insert or remove at any position
  • Use for: editing lists, removing by index
  • Returns: an array of removed items
const list = ['a', 'b', 'c'];
list.splice(1, 1, 'x'); // ['b']

๐Ÿงช Copying & Combining

slice()

  • Copy a section of an array
  • Use for: pagination, getting a range
  • Returns: a new array copy
const items = ['a', 'b', 'c', 'd'];
const page = items.slice(1, 3); // ['b', 'c']

concat()

  • Combine arrays
  • Use for: combining data from sources
  • Returns: a new merged array
const arr1 = [1, 2];
const arr2 = [3, 4];
arr1.concat(arr2); // [1, 2, 3, 4]

spread (...)

  • Copy an array
  • Use for: copying, combining arrays cleanly
  • Returns: a new array
const a = [1, 2];
const b = [...a, 3]; // [1, 2, 3]

๐Ÿ”„ Transforming Data

map()

  • Transform every item
  • Use for: formatting prices, extracting props
  • Returns: a new transformed array
const prices = [10, 20, 30];
prices.map(p => p * 1.1); // [11, 22, 33]

filter()

  • Keep matching items
  • Use for: search results, showing active users
  • Returns: a new filtered array
const users = [{active: true}, {active: false}];
users.filter(u => u.active); // [{active: true}]

reduce()

  • Combine into one value
  • Use for: totals, building objects
  • Returns: a single value
const nums = [1, 2, 3];
nums.reduce((a, b) => a + b, 0); // 6

๐Ÿ” Searching

find()

  • Find first matching item
  • Use for: find user by ID
  • Returns: the first matching element or undefined
users.find(u => u.id === 1);

findIndex()

  • Find index of first match
  • Use for: locate item to update
  • Returns: the index or -1 if not found
users.findIndex(u => u.id === 1);

indexOf()

  • Find index of exact value
  • Use for: check simple value
  • Returns: the index or -1 if not found
['a', 'b', 'c'].indexOf('b'); // 1

includes()

  • Check if value exists
  • Use for: validating permissions, checking tags
  • Returns: a boolean
['read', 'write'].includes('write'); // true

โœ… Testing

some()

  • Any item matches
  • Use for: has at least one error, any admin
  • Returns: a boolean
users.some(u => u.isAdmin); // true or false

every()

All items match Use for: all fields valid, all tasks complete Returns: a boolean

tasks.every(t => t.done); // true or false

๐Ÿ”  Ordering & Display

sort()

  • Sort array in order
  • Use for: alphabetizing names, sorting by date
  • Returns: the sorted array (same reference)
names.sort((a, b) => a.localeCompare(b));

reverse()

  • Reverse order of array
  • Use for: newest first, reverse process
  • Returns: the reversed array (same reference)
[1, 2, 3].reverse(); // [3, 2, 1]

join()

  • Join array into string
  • Use for: CSVs, building URLs
  • Returns: a string
['a', 'b', 'c'].join(','); // "a,b,c"

โš™๏ธ Other Useful Methods

forEach()

  • Run for each item
  • Use for: logging, DOM updates
  • Returns: undefined
items.forEach(i => console.log(i));

Array.from()

  • Convert to array
  • Use for: DOM NodeLists, Sets, strings
  • Returns: a new array
Array.from('hi'); // ['h', 'i']

Disclaimer: The information provided on this website is for educational and informational purposes only. Health-related content is not intended to serve as medical advice, diagnosis, or treatment recommendations and should not replace consultation with qualified healthcare professionals. Financial content is for educational purposes only and does not constitute financial advice, investment recommendations, or professional financial planning services. Always consult with licensed healthcare providers for medical concerns and qualified financial advisors for personalized financial guidance.