Did you know Object.keys() works on arrays too? 🤔 Let’s see what happens when you skip indexes!
In this JS trick, we're adding a value only at index 2 and then using Object.keys() to get the keys.
You won’t believe what gets logged 😲👇
Explanation :
arr[2] = 'x' sets a value at index 2, skipping 0 and 1.
Now, arr becomes a sparse array.
Object.keys(arr) returns only the keys that actually exist, i.e., "2".
🔍 Object.keys() ignores empty or unassigned indices — that's why it only logs "2", not "0", "1", "2".
This is a common interview question and a deep dive into how JS arrays are just special objects!
#JavaScriptTips #WebDevelopment #CodeTricks #FrontendMagic #JSInterviewQuestions #LearnJavaScript #CodingShorts #DevTips #JavaScriptHacks #100DaysOfCode