Most Asked JavaScript Interview Question: Flatten a Nested Array (Without Using .flat()) There are two common ways to flatten an array in JavaScript: 1️⃣ Using the built-in .flat() method 2️⃣ Using recursion (important for interviews) Using Recursion let arr = [[1, 2, 3, 4, 1, 2], 3, 4, [3, 1], 3, 4, 5]; let flat = []; function flatArrayFunc(arr, flat) { for (let i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { flatArrayFunc(arr[i], flat); } else { flat.push(arr[i]); } } } flatArrayFunc(arr, flat); console.log(flat); #JavaScript #CodingInterview #FrontendDeveloper #WebDevelopment
Flatten Nested Array in JavaScript Without Using .flat()
More Relevant Posts
-
💡 JavaScript Interview Trap — Can You Predict the Output? for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, 1000); } 🤔 What do you think this will print? Most developers expect: 👉 0 1 2 3 4 But the actual output is: 👉 5 5 5 5 5 🚨 Why does this happen? Because: var is function-scoped, not block-scoped By the time setTimeout runs, the loop has already completed So i becomes 5 for all executions 🧠 Fix it using let (block scope): for (let i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, 1000); } ✅ Output: 👉 0 1 2 3 4 🔥 Key Takeaways: Understand var vs let scope Know how closures work Be careful with async functions inside loops 📌 This is one of the most common JavaScript interview questions! #JavaScript #WebDevelopment #CodingInterview #Frontend #AsyncJavaScript #LearnToCode
To view or add a comment, sign in
-
🚀 Just published a new article! 📌 String Polyfills and Common Interview Methods in JavaScript If you're preparing for interviews or want to strengthen your JavaScript fundamentals, this one’s for you. In this article, I covered: • What string methods are & how they actually work • Why developers write polyfills • Implementing custom string utilities • Common interview problems & logic • Importance of understanding built-in behavior 💡 Focus is on logic over memorization — exactly what interviewers look for. 🔗 Read here: https://lnkd.in/gCcdUbMn #JavaScript #WebDevelopment #CodingInterview #MERN #Backend #LearnInPublic #100DaysOfCode
To view or add a comment, sign in
-
🚀 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 — 𝗢𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗠𝗼𝘀𝘁 𝗔𝘀𝗸𝗲𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀! If you’ve ever prepared for a JavaScript interview, you’ve definitely come across closures. But do you truly understand them? In simple terms: A closure is when a function “remembers” the variables from its outer scope even after that outer function has finished executing. 𝗪𝗵𝘆 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝗲𝗿𝘀 𝗹𝗼𝘃𝗲 𝘁𝗵𝗶𝘀 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻? Because it tests your understanding of: • Scope • Lexical environment • Memory behavior in JavaScript Quick Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 Here, inner() still has access to count even after outer() is executed — that’s a closure! Checkout the full video here: 👉https://lnkd.in/gBkyP54r Follow Alpna P. for more related content! 🤔 Having Doubts in technical journey? 🚀 Book 1:1 session with me : https://lnkd.in/gQfXYuQm 🚀IG: https://lnkd.in/gTQhjM_5 🚀 Get Complete React JS Interview Q&A Here: https://lnkd.in/d5Y2ku23 🚀 Get Complete JavaScript Interview Q&A Here: https://lnkd.in/d8umA-53 #JavaScript #Closures #WebDevelopment #Frontend #CodingInterview #LearnToCode #Developers #CodeWithAlpana Gaurav Patel
To view or add a comment, sign in
-
🚨 Stop scrolling if you keep forgetting JavaScript syntax. Ever blanked out on whether it’s .slice() or .substring()… Or struggled to recall a try...catch block during an interview? You’re not alone and that’s exactly why I created this 🚀 JavaScript Last-Minute Cheatsheet designed to save you hours of confusion and last-minute Googling. 💡 Inside the guide: ☑️ Essentials → Data types, operators, conditionals ☑️ Core Methods → Strings, Arrays, Math (with examples) ☑️ DOM Mastery → Select, create & update elements easily ☑️ Advanced JS → Promises, async/await, closures (simplified) Whether you're preparing for interviews or building real projects, this cheatsheet helps you revise faster & code with confidence. Follow M. WASEEM ♾️ for more valuable content #JavaScript #WebDevelopment #CodingCheatsheet #SoftwareEngineering #ProgrammingTips #LearnToCode #Developers #TechCommunity
To view or add a comment, sign in
-
JavaScript interview questions I see being asked again and again Although these are basics, they are often overlooked while preparing for more complex topics. These are some of the most commonly asked JavaScript questions across interviews, especially when the focus is on fundamentals and real-world understanding. 1. What is the event loop in JavaScript? 2. What is the difference between "var", "let", and "const"? 3. What is closure and how is it used? 4. What is hoisting in JavaScript? 5. What is the difference between "==" and "==="? 6. What are promises and how do they work? 7. What is the difference between synchronous and asynchronous code? 8. What is "this" in JavaScript and how does it behave? 9. What is the difference between "map", "filter", and "reduce"? 10. What is prototypal inheritance? 11. What are higher-order functions? 12. What is debouncing and throttling? 13. What is the difference between "null" and "undefined"? 14. What is the difference between "call", "apply", and "bind"? 15. What is a polyfill? Can you write a polyfill for "bind"? 16. What is the difference between shallow copy and deep copy? 17. What is event delegation? 18. How does "setTimeout" work under the hood? 19. What is the difference between "setTimeout" and "setImmediate"? 20. What are microtasks and macrotasks? 21. What is currying in JavaScript? 22. What is memoization? 23. What is the difference between "Object.freeze" and "Object.seal"? 24. How does garbage collection work in JavaScript? Strong fundamentals in JavaScript often make a noticeable difference in how you approach real-world problems. Which of these do you find most interesting or tricky? #javaScript #webdevelopment #programming #softwareengineering #InterviewPrep #interview
To view or add a comment, sign in
-
JavaScript interview questions I see being asked again and again Although these are basics, they are often overlooked while preparing for more complex topics. These are some of the most commonly asked JavaScript questions across interviews, especially when the focus is on fundamentals and real-world understanding. 1. What is the event loop in JavaScript? 2. What is the difference between "var", "let", and "const"? 3. What is closure and how is it used? 4. What is hoisting in JavaScript? 5. What is the difference between "==" and "==="? 6. What are promises and how do they work? 7. What is the difference between synchronous and asynchronous code? 8. What is "this" in JavaScript and how does it behave? 9. What is the difference between "map", "filter", and "reduce"? 10. What is prototypal inheritance? 11. What are higher-order functions? 12. What is debouncing and throttling? 13. What is the difference between "null" and "undefined"? 14. What is the difference between "call", "apply", and "bind"? 15. What is a polyfill? Can you write a polyfill for "bind"? 16. What is the difference between shallow copy and deep copy? 17. What is event delegation? 18. How does "setTimeout" work under the hood? 19. What is the difference between "setTimeout" and "setImmediate"? 20. What are microtasks and macrotasks? 21. What is currying in JavaScript? 22. What is memoization? 23. What is the difference between "Object.freeze" and "Object.seal"? 24. How does garbage collection work in JavaScript? Strong fundamentals in JavaScript often make a noticeable difference in how you approach real-world problems. Which of these do you find most interesting or tricky? #javaScript #webdevelopment #programming #softwareengineering #InterviewPrep #interview
To view or add a comment, sign in
-
🚀 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 — 𝗢𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗠𝗼𝘀𝘁 𝗔𝘀𝗸𝗲𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀! If you’ve ever prepared for a JavaScript interview, you’ve definitely come across closures. But do you truly understand them? In simple terms: A closure is when a function “remembers” the variables from its outer scope even after that outer function has finished executing. 𝗪𝗵𝘆 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝗲𝗿𝘀 𝗹𝗼𝘃𝗲 𝘁𝗵𝗶𝘀 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻? Because it tests your understanding of: • Scope • Lexical environment • Memory behavior in JavaScript Quick Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 Here, inner() still has access to count even after outer() is executed — that’s a closure! 🔥 𝗥𝗲𝗮𝗹-𝗪𝗼𝗿𝗹𝗱 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲𝘀 • Data privacy (encapsulation) • Creating counters & stateful functions • Event handlers & callbacks • Memoization & performance optimizations 💬 If you’re preparing for interviews, don’t just memorize definitions — understand how closures work under the hood. #frontend #javascript #reactjs #nextjs #community
To view or add a comment, sign in
-
🚀 JavaScript Cheat Sheet every developer should save 👇 If you're learning JavaScript or preparing for interviews… 👉 this is something you’ll keep coming back to again and again. 📚 This cheat sheet covers everything you need: ✨ Variables (var, let, const) 📦 Arrays & methods (push, pop, slice, splice) ⚙️ Functions & loops 🔢 Operators & conditions 🔤 Strings & regex 📊 Numbers, Math & Dates 🌐 DOM manipulation 🧭 Browser APIs 🖱️ Events (mouse, keyboard, forms) ⚠️ Error handling 💡 Not just basics… real-world concepts included: 👉 DOM nodes & element methods 👉 Browser window properties 👉 Event handling (🔥 interview favorite) 🔥 Why this is useful? ➡️ Quick revision before interviews ➡️ Handy while coding ➡️ Saves hours of searching 📌 Pro Tip: Don’t try to memorize everything ❌ Use it while building projects ✅ #javascript #js #frontend #interviews #interview #uideveloper #frontenddeveloper
To view or add a comment, sign in
-
#JavaScript Interview Series: Do you REALLY know how Hoisting works? Ever wondered why you can call a function before it's defined, but let and const throw an error? That's Hoisting in action! 🧐 In JavaScript, variable and function declarations are moved to the top of their scope during the compilation phase. But there's a catch... ✅ Functions: Fully hoisted. You can call them anytime. ✅ var: Hoisted but initialized as undefined. ❌ let & const: Hoisted but in the "Temporal Dead Zone" (TDZ). You can't touch them until the line of declaration. Check out the example below: console.log(a); // undefined var a = 5; greet(); // "Hello!" function greet() { console.log("Hello!"); } // console.log(b); // ReferenceError! let b = 10; Mastering these core concepts is the difference between a Junior and a Senior developer. 💻✨ Ready to ace your next JS interview? I've curated a list of the most important JavaScript questions with deep dives and Hinglish explanations! 👇 🔗 Read more here: https://lnkd.in/ghMhTcws #JavaScript #WebDevelopment #InterviewPrep #Coding #Programming #HashWebix #Frontend #ReactJS #NodeJS #TechInsights
To view or add a comment, sign in
-
-
JavaScript Interview Questions – Part 1 (Must Know for Developers!) I’ve compiled a powerful set of JavaScript concepts & interview questions that every developer should master 💡 From basics to advanced topics, this covers: -- Difference between undefined vs null -- Deep understanding of DOM & DOM Manipulation -- Event Propagation (Capturing, Target, Bubbling) -- == vs === (Most asked interview question!) -- Hoisting & Execution Context -- Closures (with real examples) -- this keyword behavior (Tricky but important!) -- Higher Order Functions & Callbacks -- Custom implementations of map, filter, reduce -- Async JavaScript (Callbacks, Promises, Async/Await) 📘 This is not just theory — it includes practical examples, edge cases, and interview-focused explanations. 💬 Whether you're preparing for interviews or strengthening your fundamentals, this will help you level up your JavaScript game. 🔥 I’ll be sharing more parts soon on WhatApp Group (https://lnkd.in/dFyqKJU3) — stay tuned! 👉 Let me know in the comments: Which JavaScript topic do you find the most confusing? #JavaScript #WebDevelopment #Frontend #Programming #Coding #InterviewPrep #Developers #LearnToCode #Tech #SoftwareEngineering #MohitDecodes
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development