What is an 'Array-like Object?' Flashcards

(10 cards)

1
Q

What is an ‘array-like object’ in JavaScript?

A

An object that has indexed access (elements can be accessed using numerical indexes like myObj[0]) and a length property, but lacks most built-in array methods (like push, pop, map, filter). It’s not a true array.

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

Give two common examples of array-like objects in JavaScript.

A
  1. The arguments object (inside non-arrow functions). 2. NodeList (from document.querySelectorAll()).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are array-like objects less convenient than true arrays?

A

They don’t have built-in array methods (like push, pop, map, filter, etc.).

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

How can you convert an array-like object into a real JavaScript array? Give two methods.

A
  1. Array.from(arrayLike, mapFn?)
  2. Spread syntax (…arrayLike)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain how to use Array.from() to convert the arguments object to a real array and double each value.

A

function myFunction() {
const argsArray = Array.from(arguments, (arg) => arg * 2);
return argsArray;
}
console.log(myFunction(1, 2, 3)); // Output: [2, 4, 6]

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

Explain how to use the spread syntax to convert a NodeList to a real array.

A

const nodeList = document.querySelectorAll(‘li’);
const nodeArray = […nodeList];

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

What are the benefits of converting an array-like object to a real array?

A

• You can use all built-in array methods.
• The code becomes more readable.
• It’s a modern JS best practice.

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

What is a potential downside to converting an array-like object?

A

Performance: Converting a very large array-like object into an array might have a slight performance cost.

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

True or false: Array-like objects inherit from Array.prototype.

A

False. They lack standard array methods because they don’t inherit from Array.prototype.

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

What happens if you try to use an array method directly on an array-like object (e.g., arguments.push(4))?

A

You’ll get a TypeError, because array-like objects don’t have Array methods until converted.

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