Javascript Flashcards

1
Q

Closure

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Splice

A

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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Map

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Filter

A

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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reduce

A

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
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Fill

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Find

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

findIndex

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

findLast

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

findLastIndex

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

flatMap

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

forEach

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

includes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

indexOf

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

join

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

keys

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

reverse

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

shift

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

some

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

sort

A

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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

unshift

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

values

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

hasOwnProperty

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

assign

A

The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
// expected output: Object { a: 1, b: 4, c: 5 }

25
Q

create

A

const person = {
isHuman: false,
func: function() { console.log(‘hello’ + name)}
}
};
const me = Object.create(person);
me.name = ‘Matthew’; // “name” is a property set on “me”, but not on “person”

26
Q

freeze

A

The Object.freeze() static method freezes an object. Freezing an object prevents extensions and makes existing properties non-writable and non-configurable. A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed, their enumerability, configurability, writability, or value cannot be changed, and the object’s prototype cannot be re-assigned. freeze() returns the same object that was passed in.

Freezing an object is the highest integrity level that JavaScript provides.
const obj = {
prop: 42
};
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode

27
Q

charAt

A

The String object’s charAt() method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.

28
Q

endsWith

A

const str1 = ‘Cats are the best!’;
console.log(str1.endsWith(‘best!’));
// expected output: true

29
Q

includes

A

The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
// returns true if it’s in the sentence

30
Q

indexOf

A

The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring. Given a second argument: a number, the method returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

31
Q

lastIndexOf

A

The lastIndexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the last occurrence of the specified substring. Given a second argument: a number, the method returns the last occurrence of the specified substring at an index less than or equal to the specified number.

32
Q

match

A

The match() method retrieves the result of matching a string against a regular expression.

const paragraph = ‘The quick brown fox jumps over the lazy dog. It barked.’;
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
// expected output: Array [“T”, “I”]

33
Q

matchAll

A

The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups.
const regexp = /t(e)(st(\d?))/g;
const str = ‘test1test2’;
const array = […str.matchAll(regexp)];
// expected output: Array [“test1”, “e”, “st1”, “1”]

34
Q

padEnd

A

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string.

const str1 = ‘Breaded Mushrooms’;

console.log(str1.padEnd(25, ‘.’));
// expected output: “Breaded Mushrooms……..”

35
Q

padStart

A

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.
const str1 = ‘5’;

console.log(str1.padStart(2, ‘0’));
// expected output: “05”

const fullNumber = ‘2034399002125581’;
const last4Digits = fullNumber.slice(-4);
const maskedNumber = last4Digits.padStart(fullNumber.length, ‘’);
// expected output: “
***5581”

36
Q

raw

A

The String.raw() static method is a tag function of template literals. This is similar to the r prefix in Python, or the @ prefix in C# for string literals. It’s used to get the raw string form of template literals — that is, substitutions (e.g. ${foo}) are processed, but escape sequences (e.g. \n) are not.
const filePath = String.rawC:\Development\profile\aboutme.html;
// expected output: “The file was uploaded from: C:\Development\profile\aboutme.html”

37
Q

repeat

A

The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

const chorus = ‘Because I'm happy. ‘;
console.log(Chorus lyrics for "Happy": ${chorus.repeat(27)}); // string 27 times

38
Q

replace

A

The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.
const p = ‘The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?’;
console.log(p.replace(‘dog’, ‘monkey’));
// expected output: “The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?”

39
Q

replaceAll

A

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
const p = ‘The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?’;

console.log(p.replaceAll(‘dog’, ‘monkey’));
// expected output: “The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?”

40
Q

search

A

The search() method executes a search for a match between a regular expression and this String object.

const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;

const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43

41
Q

slice

A

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

const str = ‘The quick brown fox jumps over the lazy dog.’;

// expected output: “the lazy dog.”

42
Q

startsWith

A

The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

const str1 = ‘Saturday night plans’;
console.log(str1.startsWith(‘Sat’)); // true

43
Q

substring

A

The substring() method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

const str = ‘Mozilla’;

console.log(str.substring(1, 3));
// expected output: “oz”

44
Q

trim

A

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.

To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd().

45
Q

trimEnd

A

The trimEnd() method removes whitespace from the end of a string and returns a new string, without modifying the original string. trimRight() is an alias of this method.

46
Q

trimStart

A

The trimStart() method removes whitespace from the beginning of a string and returns a new string, without modifying the original string. trimLeft() is an alias of this method.

47
Q

Callback

A

A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed.
function modifyArray(arr, callback) {
arr.push(100);
callback();
}
var arr = [1, 2, 3, 4, 5];
modifyArray(arr, function() {
console.log(“array has been modified”, arr);
});

48
Q

Prototype

A

A property on a function that points to an object

49
Q

Factory Function

A

A function that returns an object and can be reused to make multiple object instances

50
Q

Method

A

A property with a function as it’s value

51
Q

Iterators

A

Methods called on arrays to manipulate elements and return values.

52
Q

Syntax Error

A

SyntaxError: This error will be thrown when a typo creates invalid code — code that cannot be interpreted by the compiler. When this error is thrown, scan your code to make sure you properly opened and closed all brackets, braces, and parentheses and that you didn’t include any invalid semicolons.

53
Q

Reference Error

A

ReferenceError: This error will be thrown if you try to use a variable that does not exist. When this error is thrown, make sure all variables are properly declared.

54
Q

Type Error

A

TypeError: This error will be thrown if you attempt to perform an operation on a value of the wrong type. For example, if we tried to use a string method on a number, it would throw a TypeError.

55
Q

The heap

A

a block of memory where we store objects in an unordered manner. JavaScript variables and objects that are currently in use are stored in the heap.

56
Q

Call stack

A

Tracks what function is currently being run in your code.

57
Q

Event Queue

A

A list of messages corresponding to functions that are waiting to be processed

58
Q

Event Loop

A

A specific part of our overall event loop concept. Messages that are waiting in the event queue to be added back into the stack are added back via the event loop. When the call stack is empty, if there is anything in the event queue, the event loop can add those one at a time to the stack for execution.

First the event loop will poll the stack to see if it is empty.
It will add the first waiting message.
It will repeat steps 1 and 2 until the stack has cleared.