Array, Statements, Declarations Flashcards
filter() Method
creates a new array with all elements that pass the test implemented by the provided function. ex: const result = words.filter(word => word.length > 6); sytanx: var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
While loop
repeat a block of code until a condition is met: ex: var n = 0; while (n < 3) { n++; }
Difference between do and while and just a while loop
A do/while loop runs at least once and then checks condition: do { i = i + 1; result = result + i; } while (i < 5);
.shift()
removes the first element of an array
get the length of an array
.length
ex: array.length
unshift()
adds elements to front of array:
ex: arr.unshift(element1[, …[, elementN]])
reverse an array
The reverse() method reverses an array in place. ex: a.reverse()
Map method
The map() method creates a new array with the results of calling a provided function on every element in the calling array. ex: const map1 = array1.map(x => x * 2)
var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])
concat()
Concat method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
ex:
var new_array = old_array.concat([value1[, value2[, …[, valueN]]]])
ex: array1.concat(array2)
every() method
The every() method tests whether all elements in the array pass the test implemented by the provided function. ex: function isBelowThreshold(currentValue) { return currentValue < 40;} var array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // expected output: true
Find() method
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. ex: var found = array1.find(function(element) { return element > 10; });
forEach() method
The forEach() method executes a provided function once for each array element. ex: array1.forEach(function(element) { console.log(element); }); syn: arr.forEach(callback[, thisArg]);
includes() method
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. ex: array1.includes(2)
join() method
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.
elements.join(‘’), elements.join(), elements.join(‘-‘)
pop() method
The pop() method removes the last element from an array and returns that element. This method changes the length of the array. ex: arr.pop()
push() method
The push() method adds one or more elements to the end of an array and returns the new length of the array.
ex: animals.push(‘cows’)
reduce() method
The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.
ex: const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10
slice() method
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
ex: arr.slice([begin[, end]])
animals. slice(2, 4) or just animals.slice(2)
sort() method
The sort() method sorts the elements of an array in place and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
splice() method
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements. ex: array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
toString() method
The toString() method returns a string representing the specified array and its elements.
continue statement
The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
debugger statement
The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.
ex:
function potentiallyBuggyCode() {
debugger;
// do potentially buggy stuff to examine, step through, etc.
}
for loop
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
ex:
var str = “”;
for (var i = 0; i < 9; i++) {
str = str + i;
}
console.log(str); // expected output: “012345678”