Predict the output of the following javascript code

let array = [1, 2, 3];
array[6] = 9;
console.log(array[5]);

 

 

 

 

  1. The array array is initialized with elements [1, 2, 3].

  2. array[6] = 9; assigns the value 9 to the element at index 6. Since this index is beyond the current length of the array, JavaScript increases the size of the array to accommodate this new index.

  3. The array now looks like this: [1, 2, 3, undefined, undefined, undefined, 9].

  4. console.log(array[5]); tries to access the element at index 5. However, no value has been explicitly assigned to this index, so it returns undefined.