🚀 #100DaysOfCode – Day 2 ⚠️ This one concept breaks most JavaScript interviews: ✨ Closures 👀 Look at this: for (var i = 1; i <= 3; i++) { setTimeout(() => console.log(i), 1000); } ❓ What do you expect? → "1 2 3" 😬 What you get? → "4 4 4" 🧠 What’s happening? Closures don’t copy values… they remember variables. And "var" shares the same scope. ✅ Fix (modern JS) for (let i = 1; i <= 3; i++) { setTimeout(() => console.log(i), 1000); } 💡 In one line: Closure = function + memory of its outer scope 🔥 Why you should care? → React hooks → Event handlers → Almost every serious JS interview #JavaScript #FrontendDeveloper #100DaysOfCode #WebDevelopment
JavaScript Closures Explained with a Common Interview Trap
More Relevant Posts
-
🚀 React Interview Question Breakdown – Can You Spot the Issues? Recently, I came across some interesting React interview questions focused on debugging and code optimization. Sharing them here 👇 🔍 Question 1: Find the issues and fix them const items = useSelector(state => state.items); const [filtered, setFiltered] = useState(items); useEffect(() => { setFiltered(items.filter(i => i.name.includes(search))); }, []); 🔍 Question 2: Find the issues and fix them const handleLike = async () => { const newCount = likes + 1; setLikes(newCount); await api.updateLikes(newCount); if (condition) { setLikes(likes + 1); // Review point } }; #ReactJS #FrontendDevelopment #JavaScript #WebDevelopment #InterviewQuestions #ReactHooks
To view or add a comment, sign in
-
🚫 Think you know JavaScript? Try answering these 👇 💡 50 questions that separate beginners from pros 📉 Most developers fail here in interviews 🚀 Crack any JS interview after mastering these Top 50 JavaScript Interview Questions & Answers ⚡🔥 | Crack Frontend Interviews Easily Preparing for a JavaScript interview? This is your ultimate checklist ✅ These questions are frequently asked in frontend, full-stack, and product-based companies. 💡 Covers: ✔ Core JavaScript Concepts ✔ Closures, Hoisting, Scope ✔ Async Programming ✔ Arrays & Objects ✔ ES6+ Features ✔ Real Interview Scenarios 📚 Top 50 JavaScript Interview Questions 🔹 Basics & Core Concepts What is JavaScript? Difference between var, let, and const What is hoisting? What is scope in JS? What is a closure? Difference between == and === What are data types in JS? What is NaN? What is undefined vs null? What is type coercion? 💬 How many can you answer without Googling? 📌 Save this for your next interview 🔁 Share with your developer network 🔗 Social Links 📸 Instagram: https://lnkd.in/gW2PeGEp 🎥 YouTube: https://lnkd.in/gEB2UqRB #JavaScript #FrontendDeveloper #WebDevelopment #CodingInterview #Programming #Developers #TechJobs #LearnJavaScript #CodingTips #FullStackDeveloper #SoftwareEngineer #TechCareer #InterviewPrep #ES6 #DeveloperLife#CodeWithIndu
To view or add a comment, sign in
-
🚀 Built something for every frontend developer preparing for interviews! I’ve created a complete Preparation Guide that covers everything you actually need 👇 💡 What you’ll find inside: • 🌐 HTML, CSS & JavaScript fundamentals • ⚛️ Core concepts of React • 🧠 Machine Coding Round Questions • 📚 Structured topics & subtopics for focused learning No more random resources — everything is organized in one place to help you prepare smarter, not harder 🎯 🔗 Check it out: https://lnkd.in/geXQnzhA Would love your feedback 🙌 Let’s help each other grow 🚀 #FrontendDeveloper #React #JavaScript #WebDevelopment #CodingInterview #MachineCoding #HTML #CSS
To view or add a comment, sign in
-
-
🚫 Still confused about Lexical Scope vs Other Scopes in JavaScript? This is one of the most asked concepts in frontend interviews — and many developers still get it wrong. Let’s simplify 👇 👉 Lexical Scope (Static Scope) Functions remember where they were defined, not where they are called. That’s why inner functions can access variables from their outer functions. 👉 Types of Scope you MUST know: ✔️ Global Scope – accessible everywhere ✔️ Function (Local) Scope – inside functions only ✔️ Block Scope – inside {} (let & const) 💡 Interview Tip: If you understand how scope works with closures, you’ll crack many tricky JavaScript questions easily. 📌 In the example above: The inner function accesses outerVar because of lexical scope, not because it’s called there. 🔥 Master this → Level up your JavaScript fundamentals. 💬 Comment “SCOPE” if you want more such interview-ready posts 🔁 Share with someone preparing for frontend interviews #javascript #frontenddeveloper #webdevelopment #codinginterview #jsconcepts #100daysofcode #reactjs #developers #programming #interviewprep #techlearning #learnjavascript #scope #closures
To view or add a comment, sign in
-
-
🧠 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
-
💡 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
-
🚀 One of the MOST Asked JavaScript Interview Question ⚡“Explain Prototypal Inheritance in JavaScript” Sounds simple… but this is where most candidates get stuck 😬 Here’s the simplest way to explain it: JavaScript doesn’t use traditional class-based inheritance. Instead, it uses Prototypal Inheritance — where objects inherit from other objects. 🔥What actually happens behind the scenes? Every object is linked to another object This link is called the prototype When you try to access something: → JS first checks the object → If not found, it goes up to its prototype → Keeps going until it finds it or reaches null This is called the Prototype Chain Why interviewers ask this? Because it tests: 1.) Your core JavaScript understanding 2.) How deeply you know objects 3.) Whether you actually understand JS or just use frameworks Don't forget to follow Hrithik Garg 🚀 for more. #javascript #frontend #webdevelopment #interviewprep #coding #softwareengineer
To view or add a comment, sign in
-
🚀 One of the MOST Asked JavaScript Interview Question ⚡“Explain Prototypal Inheritance in JavaScript” Sounds simple… but this is where most candidates get stuck 😬 Here’s the simplest way to explain it: JavaScript doesn’t use traditional class-based inheritance. Instead, it uses Prototypal Inheritance — where objects inherit from other objects. 🔥What actually happens behind the scenes? Every object is linked to another object This link is called the prototype When you try to access something: → JS first checks the object → If not found, it goes up to its prototype → Keeps going until it finds it or reaches null This is called the Prototype Chain Why interviewers ask this? Because it tests: 1.) Your core JavaScript understanding 2.) How deeply you know objects 3.) Whether you actually understand JS or just use frameworks Don't forget to follow Hrithik Garg 🚀 for more. #javascript #frontend #webdevelopment #interviewprep #coding #softwareengineer
To view or add a comment, sign in
-
🔒 Advanced JavaScript — Day 5: Scope, Execution Context & Closures Today I studied one of the most important — and most misunderstood — concepts in all of JavaScript. Closures. I've heard this word thrown around in interviews, tutorials, and job descriptions for months. Today I finally sat down, understood it deeply, and built a real project using it: a fully configurable Toast Notification system. Here's everything I covered 👇 📌 Scope — Where Variables Live 📌 Execution Context & the Scope Chain 📌 Closures — The Real Magic 🪄 📌 The Toast Project — What It Does 📌 Why Closures Matter in Real Development Today was one of those days where a concept that seemed complex finally clicked completely. Closures aren't magic. They're just functions that remember where they came from. Day 6 tomorrow. The streak continues. 🔥 #AdvancedJavaScript #JavaScript #Closures #Scope #ExecutionContext #100DaysOfCode #LearnInPublic #WebDevelopment #Frontend #CodingJourney #BuildInPublic #ProjectBased #TechLearning
To view or add a comment, sign in
-
-
15 JavaScript interview questions. Spread, Rest & Default Values. Answer karo comments mein 👇 Spread Operator Q1. What is the spread operator in JavaScript? Q2. How do you use spread to copy an array without mutation? Q3. What happens when you spread two objects with duplicate keys? Q4. How is spread used in React state updates? Rest Operator Q5. What is the rest operator and how is it different from spread? Q6. What are the rules for using the rest parameter? Q7. What is the difference between rest parameters and the arguments object? Default Values Q8. What are default parameter values in JavaScript? Q9. When does a default value NOT trigger? Q10. How do default values work with destructuring? Advanced Q11. What is the difference between shallow copy and deep copy with spread? Q12. How do you use spread to pass all props in React? Q13. What is the difference between these two? js const b = [...a]; const d = c; Q14. Can you use spread with strings? Q15. What is the practical difference between these two? js function a(x, y, z) {} function b(...args) {} Full answers + code on GitHub 👇 https://lnkd.in/dj72-XEi #JavaScript #JStoReact #InterviewPrep #WebDevelopment #Frontend #ReactJS #30DayChallenge #JavaScriptTips #FrontendDeveloper #100DaysOfCode
To view or add a comment, sign in
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
Great to see 👍