Javascript Flashcards
Closure
A closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
Splice
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. ex:
const months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// Inserts at index 1
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “June”]
months.splice(4, 1, ‘May’);
// Replaces 1 element at index 4
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “May”]
Map
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
const map1 = array1.map(x => x * 2);
Filter
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
const words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];
const result = words.filter(word => word.length > 6);
// expected output: Array [“exuberant”, “destruction”, “present”]
Reduce
The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
const arr = [1, 2, 3, 4];
const initialValue = 0;
const sumArr = arr.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);
Fill
The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.
const array1 = [1, 2, 3, 4];
// Fill with 0 from position 2 until position 4
// expected output: Array [1, 2, 0, 0]
Find
The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
const arr = [5, 12, 8, 130, 44];
const found = arr.find(element => element > 10);
// 12
findIndex
The findIndex() method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
// 3
findLast
The findLast() method iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.
const array1 = [5, 12, 50, 130, 44];
const found = array1.findLast((element) => element > 45);
// 130
findLastIndex
The findLastIndex() method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
const array1 = [5, 12, 50, 130, 44];
const isLargeNumber = (element) => element > 45;
// expected output: 3
flatMap
The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1 (arr.map(…args).flat()), but slightly more efficient than calling those two methods separately.
const arr1 = [1, 2, [3], [4, 5], 6, []];
const flattened = arr1.flatMap(num => num);
// expected output: Array [1, 2, 3, 4, 5, 6]
forEach
The forEach() method executes a provided function once for each array element.
const arr = [‘a’, ‘b’, ‘c’];
arr.forEach(element => console.log(element) // a, b, c
includes
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const arr = [1, 2, 3];
arr.includes(2); // expected output: true
indexOf
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
const beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf(‘bison’));
// expected output: 1
join
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
const elements = [‘Fire’, ‘Air’, ‘Water’];
console.log(elements.join());
// expected output: “Fire,Air,Water”
keys
The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
const array1 = [‘a’, ‘b’, ‘c’];
const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// expected output: 0
// expected output: 1
// expected output: 2
reverse
The reverse() method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
shift
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
some
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.
const array = [1, 2, 3, 4, 5];
// Checks whether an element is even
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// expected output: true
sort
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
Time complexity can’t be guaranteed.
const months = [‘March’, ‘Jan’, ‘Feb’, ‘Dec’];
months.sort();
console.log(months);
// expected output: Array [“Dec”, “Feb”, “Jan”, “March”]
unshift
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const arr = [1, 2, 3];
console.log(arr.unshift(4, 5));
console.log(arr);
// expected output: Array [4, 5, 1, 2, 3]
values
The values() method returns a new array iterator object that iterates the value of each index in the array.
const array1 = [‘a’, ‘b’, ‘c’];
const iterator = array1.values();
for (const value of iterator) {
console.log(value);
}
// expected output: “a”
hasOwnProperty
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
const object1 = {};
object1.property1 = 42;
console.log(object1.hasOwnProperty(‘property1’));
// expected output: true