Javascript
Mamta Kumawat  

JavaScript Interview Question – What will be the output of the following Code?

const arr = [1, 2, 3];
arr[10] = 10;

console.log(arr.length);

In JavaScript, arrays are a common data structure used to store collections of elements. Typically, when you add elements to an array, JavaScript will automatically update the array’s length property. At first glance, this code might look like it would simply add a 10 to the end of the array, but there’s more going on here than meets the eye. Let’s break it down and understand the behavior of JavaScript arrays when dealing with index assignments that are beyond the current length. This is a typical JavaScript Interview Question that tests your understanding of array behavior. This specific question often comes up in technical interviews for JavaScript developers.

What is Happening in the Code?

Common JavaScript Interview Questions

Array Initialization:
The array arr is initialized with three element:

const arr = [1, 2, 3];

At this point, the array looks like:

[1, 2, 3]

Its length property is 3 because there are three elements in the array.

Assigning a Value to an Index Beyond the Current Length:
The next line of code is where the magic happens:

arr[10] = 10;

Here, we are setting the element at index 10 to be 10. This is an interesting move because the array currently only has three elements (at indices 0, 1, and 2).

What does JavaScript do?
When you assign a value to an index that is larger than the current length of the array, JavaScript will create “empty slots” in the array from the current length up to the assigned index. In this case, JavaScript will create empty slots at indices 3, 4, 5, …, 9. These slots will be empty (i.e., undefined), and the array will automatically increase its length property to reflect the highest index used, which is 10 in this case.

So, the array now looks like

[1, 2, 3, <7 empty items>, 10]

The length property of the array has been automatically updated to 11 because the array now spans from index 0 to index 10, inclusive.

Logging the Array Length:
Finally, the code logs the length of the array

console.log(arr.length);

The output will be:

11

This is the result of the array now having a total of 11 elements, with the positions from index 3 to 9 being empty (but still counted in the length).

Key Points to Understand

  • Arrays in JavaScript Are Sparse:
    JavaScript arrays do not automatically fill in gaps when you assign a value to an index that is out of the current array bounds. Instead, it will create “empty” slots for the indices between the current length and the new index you’re setting. These “empty” slots are not undefined elements but are considered holes in the array, which are counted toward the length of the array.
  • The length Property:
    The length property of an array is automatically updated to reflect the highest index plus one. So, if you assign a value to an index that is larger than the current length, JavaScript will increase the length property to accommodate that new index, even if the intermediate indices are empty.
  • Sparse Arrays and Array Methods:
    Sparse arrays (arrays with “empty” slots) can sometimes lead to unexpected behavior when using array methods like map(), forEach(), or reduce(). These methods will skip over the empty slots and not process them. It’s important to be aware of this behavior when working with arrays that have been sparsely populated

Conclusion

Understanding how JavaScript handles array indexing, especially when assigning values to out-of-bounds indices, can help you avoid unexpected behaviors in your code. In the example above, assigning a value to index 10 on an array that initially has three elements leads to a sparse array, where empty slots are created between the last index and the new one, and the length property is adjusted accordingly.

While this behavior is part of JavaScript’s flexibility, it’s important to be mindful of sparse arrays, as they can introduce performance issues and complicate your code when using array methods.

Leave A Comment