collections Flashcards

1
Q

What are collections?

A

collections are made up of individual elements. In JS this includes arrays, objects, and kinda strings

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

Calling theslicemethod without any arguments will return…

A

a copy of the original string

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

The first argument of slice specifies…

A

the index at which to start the extraction

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

The second argument of slice specifies…

A

the index where you want to end the extraction. The character at the ending index isn’t part of the returned substring.

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

sliceandsubstringdiffer…

A
  • When the start index is greater than the end index,substringswaps the two arguments whileslicereturns an empty string:
  • When either argument is negative,substringtreats them as0, while, as we saw above,slicetreats them aslength + index:
  • We recommend usingString.prototype.slice
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When given negative numbers as the indices,slicetreats them as

A

string length + index. IE, it counts backward from the last char (-1 is the last char)

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

String.prototype.slicereturns…

A

a new string

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

Array.prototype.slicereturns…

A

a new array

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

Callingarr.slicewithout any arguments returns…

A

a shallow copy of the original array

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

Referencing an out-of-bounds index with slice returns…

A

undefined

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

Object keys are also called…

A

properties

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

Accessing an index less than0on an array or a string returns…

A

undefined

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

to check if that property exists and simply holds undefined, or does not exist, we can use…

A

let obj = { a: ‘foo’, b: ‘bar’, c: undefined};
Object.keys(obj).includes(‘c’); // => true
//OR
obj.hasOwnProperty(‘c’); // => true
obj.hasOwnProperty(‘d’); // => false

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

Using a key to access a property that doesn’t exist on an object returns…

A

undefined

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

We can access just the keys or just the values from an object with

A

theObject.keysandObject.valuesmethods. These methods return an array

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

let capitals = { uk: ‘London’, france: ‘Paris’, germany: ‘Berlin’ };
Object.keys(capitals); // => [‘uk’, ‘france’, ‘germany’]
Object.values(capitals); // => [‘London’, ‘Paris’, ‘Berlin’]

//Whats happening here?
Object.values(capitals)[0];

A

// => ‘London’
We are making an array element reference because object.values returns an array

14
Q

JavaScript arrays are objects. The chief difference between an array and some other object is that…

A

it uses non-negative integers as its primary keys.

15
Q

What happens to an array when you add elements?

A

the array increases the value of itslengthproperty

15
Q

changing the value of thelengthproperty on an array causes…

A

the number of elements to change.

15
Q

What happens if you add add new properties to an array just as you would any other object

A

these properties are NOT elements of the array but ordinary properties and these properties will not be counted in arr.length. Those properties are are also ignored by array methods likeforEach,map, andfilter

16
Q

if you have empty elements from manually lengthening an array, iterative array methods will…

A

skip over these

16
Q

when we use anObjectmethod, such askeys on an array with non element properties…

A

we get a list of all of the property names

16
Q

when we use anObjectmethod, such askeys on an array with unset elements…

A

keys ignores the unset elements

17
Q

String.prototype.split, when called without any arguments, returns…

A

an array with the string as its only element

18
Q

When called without any arguments,Array.prototype.joinreturns…

A

a string with the elements of the array joined together into a string, separated by commas.

19
Q

Apart from theObject.keysandObject.valuesmethods, you can use_______________ to convert an object to an array.

A

Object.entries

20
Q

Can you can increment an already set array value?

A

Yes,
let numbers = [1, 2, 3, 4];
numbers[0] = numbers[0] + 1; // => 2
numbers; // => [ 2, 2, 3, 4 ]

21
Q

What happens if you increment an unset array index?

A

you end up with a NaN in that element

22
Q

string element reassignment…

A

is syntactically permitted, but doesn’t affect the string