JavaScript Interview Question: var vs let vs const (Theory) Q: What is the difference between var, let, and const in JavaScript? The difference comes down to scope, hoisting, and reassignment behavior. A) var 1) Function-scoped 2) Hoisted and initialized with undefined 3) Can be redeclared and reassigned 4) Can cause bugs due to scope leakage B) let 1) Block-scoped 2) Hoisted but in the Temporal Dead Zone (TDZ) 3) Can be reassigned but not redeclared in the same scope C) const 1) Block-scoped 2) Hoisted but in TDZ 3) Cannot be reassigned 4) Object properties can still be mutated 📌 Best Practice: Use const by default, let when reassignment is needed, and avoid var in modern JavaScript. #JavaScript #WebDevelopment #FrontendDevelopment #MERNStack #InterviewPreparation #SoftwareEngineering #Developers
JavaScript var vs let vs const: Scope, Hoisting, and Reassignment
More Relevant Posts
-
🔎 Difference Between undefined and not defined in JavaScript These two terms sound similar, but they mean very different things in JavaScript — and they often show up in interviews! 1️⃣ undefined A variable is undefined when it has been declared but hasn’t been assigned a value yet. let a; console.log(a); // undefined ✔️ The variable exists ✔️ Memory is allocated ❌ No value assigned yet 2️⃣ not defined A variable is not defined when it has never been declared in the current scope. console.log(b); // ReferenceError: b is not defined ❌ The variable does not exist ❌ No memory allocated ❌ JavaScript throws an error 💡 Interview Tip Even var variables are undefined during hoisting: console.log(x); // undefined var x = 10; But this will throw an error: console.log(y); // y is not defined #JavaScript #WebDevelopment #Frontend #CodingInterview #LearnToCode
To view or add a comment, sign in
-
🚀 Day 38/365 – Top JavaScript Interview Question Asked in Companies 🔥 const a = { b: 10, c: 20, }; const b = { c: 30 }; a[b] = { d: 40 }; console.log(a); ✅Output: { b: 10, c: 20, "[object Object]": { d: 40 } } 💡 Why does this happen? In JavaScript, object keys can only be strings or symbols. When we use an object (b) as a key: a[b] = { d: 40 }; JavaScript automatically converts that object into a string using .toString(). And when an object is converted to string, it becomes: "[object Object]" So internally, JavaScript does this: a["[object Object]"] = { d: 40 }; That’s why the final object contains a new key called: "[object Object]" #JavaScript #FrontendDeveloper #WebDevelopment #JSInterview #CodingInterview #365DaysOfCode #SoftwareEngineer #LearnToCode #Developers #TechCareers
To view or add a comment, sign in
-
💼 JavaScript Interview Question I Cracked Today ❓ “Explain the Event Loop in JavaScript.” My answer (simple & interview-ready): 🧠 JavaScript is single-threaded, but it handles async tasks using the Event Loop. How it works: 1️⃣ Call Stack executes synchronous code 2️⃣ Async tasks go to Web APIs 3️⃣ Promises move to the Microtask Queue 4️⃣ setTimeout goes to the Callback Queue 5️⃣ Event Loop pushes microtasks first, then callbacks 💡 Interview Tip: Even setTimeout(fn, 0) runs after Promises. This question tests: ✔ Async understanding ✔ Execution order ✔ Real-world debugging skills If you’re preparing for frontend interviews, master this one concept — it shows up everywhere. Learning → Practicing → Explaining = Growth 🚀 #JavaScript #EventLoop #InterviewPrep #FrontendDeveloper #WebDevelopment #DevTips
To view or add a comment, sign in
-
🚀 Mastering JavaScript Prototypes for Interviews As part of my frontend interview preparation, I revisited one of the most important core JavaScript concepts — Prototypes & Prototype Inheritance. Here’s a quick breakdown 👇 🔹 What is a Prototype? Every JavaScript object has an internal [[Prototype]] reference. When a property is not found on an object, JavaScript looks up the prototype chain. 🔹 Why Prototypes Matter? ✔ Memory optimization (shared methods) ✔ Inheritance implementation ✔ Core foundation behind ES6 classes 🔹 Prototype Chain Example _______________________________________ function User(name) { this.name = name; } User.prototype.greet = function() { return `Hi ${this.name}`; }; const user1 = new User("Shravanthi"); console.log(user1.greet()); ________________________________________ 🔹 Important Interview Topics • __proto__ vs prototype • Object.create() • Constructor functions • ES6 classes (syntactic sugar over prototypes) • Property lookup mechanism • Object.freeze() vs Object.seal() 💡 Key takeaway: Understanding prototypes deeply helps in debugging, writing optimized code, and explaining how JavaScript works under the hood. #JavaScript #FrontendDevelopment #InterviewPreparation #ReactJS #WebDevelopment #LearningJourney
To view or add a comment, sign in
-
🚀 JavaScript Interview Classic: Product of Array Except Self (No Division!) You’re given an array like: [1, 2, 3, 4] 👉 Build a new array where each index contains the product of all numbers except itself: ✅ Output → [24, 12, 8, 6] But here’s the catch 👀 ❌ No division ✅ Must run in O(n) time 🧠 Think in “Left” and “Right” products Instead of multiplying everything again and again, ask: At index i: ➡️ What’s the product of all elements on the left? ⬅️ What’s the product of all elements on the right? Then: answer[i] = leftProduct × rightProduct 🎯 JavaScript Solution (Optimized: O(n) time, O(1) extra space) function productExceptSelf(nums) { const n = nums.length; const result = new Array(n).fill(1); // 1) Build prefix products into result let prefix = 1; for (let i = 0; i < n; i++) { result[i] = prefix; // product of all elements to the left of i prefix *= nums[i]; } // 2) Multiply postfix products into result let postfix = 1; for (let i = n - 1; i >= 0; i--) { result[i] *= postfix; // multiply by product of all elements to the right of i postfix *= nums[i]; } return result; } ✅ Handles zeros correctly ✅ No division ✅ Interview-approved 🧩 Mini Walkthrough (Super Quick) For [1,2,3,4] Prefix pass builds: [1, 1, 2, 6] Postfix pass multiplies: → [24, 12, 8, 6] 🔥 If you want to level up: Can you do it in one loop? 😉 Hint: Two pointers i and j. #DataStructures #Algorithms #JavaScript #CodingInterview #LeetCode #ProblemSolving #WebDevelopment #SoftwareEngineering #Programming #DeveloperCommunity #100DaysOfCode #FrontendDevelopment
To view or add a comment, sign in
-
🚀 JavaScript Interview Quick Revision Q. What is the difference between localStorage and sessionStorage? Answer: Persistence duration — localStorage persists indefinitely; sessionStorage clears on tab close. Q. What is the difference between document.ready and window.onload? Answer: document.ready triggers when DOM is ready; window.onload when all assets load. Q. What are JavaScript timers? Answer: Functions like setTimeout and setInterval to schedule code execution. Q. What is debouncing? Answer: Technique to limit how often a function is called. Q. What is throttling? Answer: Technique to make sure a function is called at most once per interval. 📘 These are part of my 3000+ JavaScript Interview Questions & Answers book. If you're preparing for frontend interviews, structured revision matters. Comment “JS” if you want the book link.
To view or add a comment, sign in
-
JavaScript Interview Question: What is the difference between Debouncing and Throttling? Answer: Both techniques control how often a function executes during frequent events. Debouncing: Executes a function after the user stops triggering the event. Throttling: Executes a function at fixed intervals while the event continues. Example (Debounce): 𝘧𝘶𝘯𝘤𝘵𝘪𝘰𝘯 𝘥𝘦𝘣𝘰𝘶𝘯𝘤𝘦(𝘧𝘯, 𝘥𝘦𝘭𝘢𝘺){ 𝘭𝘦𝘵 𝘵𝘪𝘮𝘦𝘳 𝘳𝘦𝘵𝘶𝘳𝘯 𝘧𝘶𝘯𝘤𝘵𝘪𝘰𝘯(){ 𝘤𝘭𝘦𝘢𝘳𝘛𝘪𝘮𝘦𝘰𝘶𝘵(𝘵𝘪𝘮𝘦𝘳) 𝘵𝘪𝘮𝘦𝘳 = 𝘴𝘦𝘵𝘛𝘪𝘮𝘦𝘰𝘶𝘵(𝘧𝘯, 𝘥𝘦𝘭𝘢𝘺) } } Explanation: Debouncing is useful for: 1. search inputs 2. auto-save features Throttling is useful for: 1. scroll events 2. window resizing Follow-up Interview Question: Why is debouncing important for API calls? Answer: Because it prevents multiple unnecessary API requests during rapid input changes. #javascript #WebPerformance #FrontendOptimization #SoftwareEngineering
To view or add a comment, sign in
-
📌 Mock Interview Question – JavaScript Q: What is catch in JavaScript? In JavaScript, catch is used with try to handle errors in a program. When we write code inside a try block, JavaScript will try to execute it. If an error occurs, the catch block will handle the error so that the program does not stop suddenly. 🔹 Syntax: try { // code that may cause error } catch (error) { // code to handle the error } 🔹 Example: try { let result = x + 10; // x is not defined } catch (error) { console.log("Error occurred:", error.message); } 👉 Output: Error occurred: x is not defined 💡 Why use catch? Prevent program crash Handle errors gracefully Show proper messages to users #JavaScript #WebDevelopment #FrontendDeveloper #CodingJourney #LearningToCode
To view or add a comment, sign in
-
𝗔𝗱𝘃𝗮𝗻𝗰𝗲𝗱 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀 𝗳𝗼𝗿 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝘀 — 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗣𝗿𝗲𝗽𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗚𝘂𝗶𝗱𝗲 Master advanced JavaScript concepts that are essential for cracking frontend and full stack developer interviews. This guide covers closures, scope, hoisting, event loop, promises, async/await, prototypes, execution context, memory management, debouncing, throttling, and performance optimization — explained with real-world examples. Perfect for developers aiming to build deep JavaScript expertise and succeed in technical interviews. #JavaScript #AdvancedJavaScript #FrontendDevelopment #WebDevelopment #JavaScriptInterview #Programming #SoftwareEngineering #CodingInterview #LearnJavaScript #FullStackDeveloper
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