JavaScript Flashcards

1
Q

Array.prototype.at(index)

A

Takes an integer value. Allowing for positive and negative integers.
Returns the item at that index or undefined if index can’t be found.
Negative integers count back from the last item in the array. (Similar to array[0] or array[array.length-1] but can use .at(-1))

Example: 
const array1 = [1, 2, 3];
let index = 2;
array1.at(index); //expected output: 3
index = -2;
array1.at(index); //expected output: 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array.prototype.length

A

Property of an object which is an instance of type Array sets or returns the number of elements in the array.

Example:
const array1 = [1,2,3];
array1.length; //e.o: 3

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

Array.prototype.concat(array2)

A

Merges two or more arrays. Doesn’t change the existing arrays. Returns a NEW array.

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

Objects

A

Datatype. Stores various keyed collections and more complex entities.

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

Array.prototype.every(callback(element, index, array))

A

Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. If the callback returns truthy for every element, every returns true. Otherwise returns false if just element fails.

*Calling this method on an empty array will return true for any condition!

Example:
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

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

Array.prototype.filter(callback(element, index, array))

A

Creates a new array with all elements that pass the test implemented by the provided function. So if the test returns true, great, but if it returns false, the new array doesn’t include that element that failed the test.

Example:
const result = words.filter(word => word.length > 6);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array.prototype.forEach(callback(element, index, array))

A

Returns nothing (undefined). Executes a provided function once for each array element it loops through.

*There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.

Early termination may be accomplished with:

A simple for loop
A for...of / for...in loops
Array.prototype.every()
Array.prototype.some()
Array.prototype.find()
Array.prototype.findIndex()

Array methods: every(), some(), find(), and findIndex() test the array elements with a predicate returning a truthy value to determine if further iteration is required.

*forEach expects a synchronous function.

forEach does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback.

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

Array.prototype.includes(searchElement, fromIndex)

A

Returns true/false. Loops through each element and checks if the value passed matches the element.

Example:
[1, 2, 3].includes(2)         // true
[1, 2, 3].includes(4)         // false
[1, 2, 3].includes(3, 3)      // false
[1, 2, 3].includes(3, -1)     // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Array.prototype.shift()

A

Removes the first element from an array and returns that removed element, or undefined if the array is empty. This method changes the length of the array.

Example: 
const firstElement = array1.shift();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a closure?

A

Combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, 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.

Every closure has three scopes:

Local Scope (Own scope)
Outer Functions Scope
Global Scope

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

What is scope?

A

Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global:

  1. Global variables are those declared outside of a block (var, let outside of functions)
  2. Local variables are those declared inside of a block

Local scope can be function-scoped (var) or block-scoped (let, const) within (also) functions, if, for, while loop.

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

What does null mean in JS?

A

A keyword in JavaScript that signifies ‘no value’ or nonexistence of any value. Assign a variable to null to shed off it’s assigned value. ‘Null’ is an object with a valid value, no properties, is non-mutable, and only a single instance of the same exists in the system at all time.

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

What is undefined?

A

A global variable that JavaScript creates at run time. This global variable is assigned to an object in one of the following cases:
1. Declared but not defined – JavaScript assigns ‘undefined’ to any object that has been declared but not initialized or defined. In other words, in a case where no value has been explicitly assigned to the variable, JavaScript calls it ‘undefined’.

  1. Array index or object property that does not exist.
  2. A function parameter that has not been supplied.
  3. The return value of functions that have to but don’t return a value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Hoisting

A

A behavior of JavaScript in which variable and function declarations are moved to the top of their scope before execution of the script. Only the actual declaration (var x) is hoisted, not the initialization (x=10).

https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript

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