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”
for…in statement
The for...in statement iterates over all non-Symbol, enumerable properties of an object. ex: var string1 = ""; var object1 = {a: 1, b: 2, c: 3}; for (var property1 in object1) { string1 += object1[property1]; } console.log(string1); // expected output: "123"
for…of statement
The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object. ex: let iterable = [10, 20, 30]; for (const value of iterable) { console.log(value); } // 10 // 20 // 30
function declaration
The function declaration (function statement) defines a function with the specified parameters. function calcRectArea(x, y) { return x* y;}
return statement
The return statement ends function execution and specifies a value to be returned to the function caller.
switch statement
The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.
use when you have a lot of else if statements
throw statement
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won’t be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.
try…catch
The try…catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
arrow function expression
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
Default function parameters
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. function multiply(a, b = 1) { return a * b; } console.log(multiply(5, 2));// expected output: 10
constructor method
The constructor method is a special method for creating and initializing an object created within a class.
A constructor can use the super keyword to call the constructor of a parent class.
If you do not specify a constructor method, a default constructor is used.
extends keyword
The extends keyword is used in class declarations or class expressions to create a class which is a child of another class. ex: class formatDate extends Date { constructor(dateStr) { super(dateStr); }
Function arguments
arguments are values that are passed into functions that receive them as parameters.. (arg= 2,4) ex: function add(a,b){ return a + b; } add(2, 4)