JavaScript Interview Question: Array Looping and hasOwnProperty

🧠 JavaScript Interview Question Here’s a small but tricky one that often comes up in interviews 👇 👉 Given an array: let array = [1, 2, 3, 4, 5]; 🚨 Step 1: Add extra properties to the array array.name = "Code"; array.role = "Frontend Developer"; for (let key in array) { console.log(key, ":", array[key]); } 👉 Output: 0 : 1 1 : 2 2 : 3 3 : 4 4 : 5 name : Code role : Frontend Developer 😮 Notice how for...in also loops through custom properties! ✅ Step 2: Print only original array values using hasOwnProperty for (let key in array) { if (array.hasOwnProperty(key) && !isNaN(key)) { console.log(array[key]); } } 👉 Output: 1 2 3 4 5 #JavaScript #FrontendDeveloper #ReactJS #CodingInterview #WebDevelopment

To view or add a comment, sign in

Explore content categories