JavaScript Arrays, Iterables, and Collections Flashcards

(18 cards)

1
Q

When should I favor array methods (like map, filter, reduce) over traditional for loops?

A

For concise, readable transformations, filtering, aggregations, and simple iterations. They are declarative and maintainable. For loops offer more control and may be faster for large arrays or complex operations.

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

What are the advantages and disadvantages of using array methods instead of for loops?

A

Advantages: Concise, readable, declarative, chainable. Disadvantages: Potentially less performant on large datasets/complex logic, harder to debug complex chains.

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

What’s the difference between a shallow copy and a deep copy of an array?

A

Shallow Copy: New array, but elements are references to same objects. Changes to objects affect both arrays. Deep Copy: Completely new array with copies of all objects. Changes to objects only affect the copied array.

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

How can you create a deep copy of an array in JavaScript (and what are the limitations)?

A

JSON.parse(JSON.stringify(array)) - simple but doesn’t work with functions, undefined, dates, or circular refs. _.cloneDeep(array) (Lodash) - robust. Recursive function - most control but complex.

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

What methods create a shallow copy?

A

Spread syntax (…), slice(), Object.assign() when applied to arrays (performs shallow copy)

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

Why is immutability important in JavaScript?

A

Makes code more predictable, easier to debug, avoids unexpected side effects. Essential for state management in React and Redux.

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

What’s the difference between mutating and non-mutating array methods?

A

Mutating: Modify original array directly (push, pop, shift, unshift, splice, sort, reverse). Non-Mutating: Create new array or value without changing original (slice, concat, map, filter, reduce).

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

Should you prefer mutating or non-mutating methods?

A

Favor non-mutating methods whenever possible to promote immutability and avoid unexpected side effects.

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

Give examples of mutating array methods.

A

push(), pop(), shift(), unshift(), splice(), sort(), reverse(), fill()

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

Give examples of non-mutating array methods.

A

slice(), concat(), map(), filter(), reduce(), flat(), flatMap()

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

When would you choose to use an Object over an Array?

A

When storing key-value pairs with known string/Symbol keys, and you want direct access via dot notation.

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

When would you choose to use a Set over an Array?

A

When you need to store only unique values and quickly check for existence of a value.

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

When would you choose to use a Map over an Object?

A

When keys can be any data type (including objects), insertion order must be preserved, and frequent add/delete/update operations are needed.

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

What are the key differences between a Map and a WeakMap?

A

Map: keys any type, strong refs, iterable, has .size. WeakMap: keys must be objects, weak refs, not iterable, no .size. WeakMap keys allow garbage collection.

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

What are the key differences between a Set and a WeakSet?

A

Set: values any type, strong refs, iterable, has .size. WeakSet: values must be objects, weak refs, not iterable, no .size. WeakSet values allow garbage collection.

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

When should you use a WeakMap?

A

When associating data with objects (as keys) without preventing garbage collection. Useful for caching, DOM metadata, private data in classes.

17
Q

When should you use a WeakSet?

A

When tracking a collection of objects without preventing garbage collection. Useful for private class data, marking objects as ‘processed’, etc.

18
Q

Why don’t WeakMap and WeakSet have a .size property or support iteration?

A

Because garbage collector can reclaim objects at any time, making size unpredictable and iteration unreliable.