Skip to content

Instantly share code, notes, and snippets.

@alexcrist
Created March 1, 2021 20:51
Show Gist options
  • Save alexcrist/86569a1d8fc4a960a180f2e24140c247 to your computer and use it in GitHub Desktop.
Save alexcrist/86569a1d8fc4a960a180f2e24140c247 to your computer and use it in GitHub Desktop.
let a = [9, 2, 3];
a.length
// a.push()
// a.pop()
// a.unshift()
// a.splice()
// a.slice()
// a.reverse()
// a.join()
// a.concat()
// ===============
// Push adds a new element to the end of an array
a.push(5);
console.log(a);
// Pop removes the last element of the array
a.pop();
console.log(a);
// Unshift adds an element to the beginning of the array
a.unshift(5);
console.log(a);
// Splice can remove elements from an array
console.log('=== Splice ===');
let value = a.splice(0, 2);
console.log('Array a:', a);
console.log('Value:', value);
// Values: 5 9 2 3
// Indices: 0 1 2 3 4
// Slice will copy an array (does not modify the original array)
console.log('=== Slice ===');
a = [5, 9, 2, 3];
value = a.slice(0, 2);
console.log('Array a:', a);
console.log('Value:', value);
// ===============
console.log('=== Reverse ===');
console.log(a);
a.reverse()
console.log(a);
// ===============
console.log('=== Join ===');
const strArray = ['apple', 'blueberry', 'orange'];
console.log(strArray.join('-'));
// ===============
const array1 = ['Boston', 'Houston'];
const array2 = ['San Diego', 'Seattle', 'NYC'];
const bigArray = array1.concat(array2);
console.log(bigArray);
// ===============
console.log('=== For loops ===');
const ourArray = ['Broccoli', 'Green beans', 'Asparagus'];
for (let i = 0; i < ourArray.length; i += 2) {
console.log('index:', i);
console.log('value:', ourArray[i]);
}
console.log('=== For-of loops ===');
for (const item of ourArray) {
console.log(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment