Search This Blog

2026/08/11

Javascript:Guess Output of given code

 



Code:Guess Output of following code

Code:
const x=[];
x[4] = 1;

x.forEach((item,index,array)=>{
   console.log(`${item} at index = ${index}`);
})

Output:
1 at index = 4

Explaination:
in array x length is 5 but all element from index 0,1,2,3
are not initialized and are defined as empty,foreach loop
only over array element which are not empty hence forEach
will give console.log for only 4th index.

No comments:

Post a Comment