InterviewHubPRO
Section Overview
Q434

Question 434 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

83%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

What is the difference between dense and sparse arrays?

An array contains items at each index starting from first(0) to last(array.length - 1) is called as Dense array. Whereas if at least one item is missing at any index, the array is called as sparse.

Let's see the below two kind of arrays,

js
 const avengers = ["Ironman", "Hulk", "CaptainAmerica"];
 console.log(avengers[0]); // 'Ironman'
 console.log(avengers[1]); // 'Hulk'
 console.log(avengers[2]); // 'CaptainAmerica'
 console.log(avengers.length); // 3

 const justiceLeague = ["Superman", "Aquaman", , "Batman"];
 console.log(justiceLeague[0]); // 'Superman'
 console.log(justiceLeague[1]); // 'Aquaman'
 console.log(justiceLeague[2]); // undefined
 console.log(justiceLeague[3]); // 'Batman'
 console.log(justiceLeague.length); // 4