JavaScript reminder that hit me again today 😅 Not all “functions” in JS behave the same. Hoisting treats them completely differently depending on how you write them. // 1. Function declaration sayHi(); // works function sayHi() { console.log("Hello"); } // 2. Function expression with var sayHi(); // TypeError: sayHi is not a function var sayHi = function () {}; // 3. Function expression with let/const sayHi(); // ReferenceError const sayHi = () => {}; Same idea… totally different behavior. Function declarations get lifted fully. var just gets hoisted as undefined. And let / const sit in the “don’t touch me yet” zone. It’s wild how many bugs come from just this one thing 😅 JavaScript keeps us humble. #javascript #webdevelopment #codinglife #frontend #devhumor
JavaScript hoisting: A reminder of its quirks
More Relevant Posts
-
💥 “Understanding JavaScript Promises (Without Crying)” A Promise is exactly that: JavaScript saying “I don’t have the result yet, but I promise I’ll get back to you.” So when you write this: let pizza = new Promise((resolve, reject) => { let ovenReady = true; if (ovenReady) resolve("Pizza is ready! 🍕"); else reject("Oven broke down 💀"); }); pizza .then(result => console.log(result)) .catch(error => console.log(error)); You’re basically saying: “If all goes well — deliver the pizza 🍕.” “If not — at least tell me what went wrong 💀.” And JavaScript delivers that promise later, when it’s ready. So next time someone says “Promises are hard,” just remember — it’s literally JS saying, “Hold on, I’m cooking your pizza.” #JavaScript #WebDev #LearnToCode #CodingHumor #Frontend
To view or add a comment, sign in
-
-
Ever seen a variable live even after its function dies? That’s Closure — the memory ninja of JavaScript 🧠 One of JavaScript’s most powerful (and tricky) features 💡 Let’s look at a simple example 👇 function counter() { let count = 0; return function () { count++; console.log(count); }; } const increment = counter(); increment(); // 1 increment(); // 2 increment(); // 3 Wait… how does count still remember its value? Didn’t the counter() function finish already? 😅 Here’s the magic 🪄 > A closure allows a function to “remember” the variables from the scope in which it was created — even after that scope is gone. In this example, The inner function still has access to count, Because it closes over the variables from its outer function. That’s why we call it a Closure 🔁 Closures are the reason we can create: ✅ Private variables ✅ Function factories ✅ Modular, memory-efficient code In short — > A closure is how JavaScript gives functions memory 🧠 #JavaScript #Closures #WebDevelopment #Frontend #MERNStack #NodeJS #ReactJS #Coding #SoftwareEngineering #Developers #JSFundamentals
To view or add a comment, sign in
-
Ever seen a variable live even after its function is done? 🤔 That’s not a bug — that’s Closure, one of JavaScript’s most powerful (and tricky) features 💡 Let’s look at a simple example 👇 function counter() { let count = 0; return function () { count++; console.log(count); }; } const increment = counter(); increment(); // 1 increment(); // 2 increment(); // 3 Wait… how does count still remember its value? Didn’t the counter() function finish already? 😅 Here’s the magic 🪄 > A closure allows a function to “remember” the variables from the scope in which it was created — even after that scope is gone. In this example, The inner function still has access to count, Because it closes over the variables from its outer function. That’s why we call it a Closure 🔁 Closures are the reason we can create: ✅ Private variables ✅ Function factories ✅ Modular, memory-efficient code In short — > A closure is how JavaScript gives functions memory 🧠 #JavaScript #Closures #WebDevelopment #Frontend #MERNStack #NodeJS #ReactJS #Coding #SoftwareEngineering #Developers #JSFundamentals
To view or add a comment, sign in
-
💥 JavaScript: The Language That Makes You Say “WHAT?!” 🤯 Look at this for 5 seconds 👇 console.log([] == ![]); // true 😳 Wait… what?! How can an empty array be equal to NOT an empty array? 💀 Welcome to the wild world of JavaScript type coercion 😅 Here’s what’s actually happening: 👉 ![] → becomes false (because arrays are truthy) 👉 So now it’s [] == false 👉 JS tries to be helpful and converts both sides to numbers [] → 0 and false → 0 ✅ 0 == 0 → true BOOM 💥 mind = blown. 🧠 Pro Tip: Always use === instead of == unless you love debugging existential crises 😅 🔥 Your turn — what’s the weirdest JS behavior you’ve ever seen? Drop it below 👇 Let’s confuse the internet together. #JavaScript #Frontend #WebDev #ProgrammingHumor #CodingLife #100DaysOfCode
To view or add a comment, sign in
-
-
JavaScript Concept — “The Power of Closures” 💭 Ever wondered how JavaScript functions “remember” the variables around them? That’s the magic of Closures — one of JavaScript’s most elegant features. Closures allow a function to access variables from its outer scope, even after that scope has closed. This concept powers some of the most powerful patterns in JS — from private variables to event handlers. Here’s a small example 👇 function counter() { let count = 0; return function() { count++; return count; }; } const add = counter(); console.log(add()); // 1 console.log(add()); // 2 It’s simple, elegant, and shows how deep JavaScript really is. #JavaScript #WebDevelopment #Coding #Frontend #Learning
To view or add a comment, sign in
-
#TIL The “You Don’t Need” series is a great reality check for many long-lived JS habits. One of the classics: You don’t need Lodash anymore. Most of its utilities are now built into the language — Array.flatMap, Object.entries, String.replaceAll, and so on. And for teams who still want familiar APIs, there’s a modern alternative: ES Toolkit — smaller, faster, actively maintained, fully typed, and even Lodash-compatible. Resources worth checking: • https://lnkd.in/e_VkCk_Y • https://lnkd.in/e7wCZBQx • https://es-toolkit.dev/ Lodash/Underscore has served us well — but modern JS gives us cleaner, faster, and native ways to get the same job done. #JavaScript #Frontend #WebDevelopment #Performance #Tooling #YouDontNeedSeries
To view or add a comment, sign in
-
JavaScript thing I randomly remembered today 😅 You know how let and const throw an error if you try to use them before declaring? That weird phase has a name — Temporal Dead Zone (TDZ). console.log(a); // ❌ ReferenceError let a = 5; It’s not that a doesn’t exist. It’s there... just not initialized yet. Basically, JS says “I know you declared it, but don’t touch it till I’m ready.” 😂 var doesn’t have this — which is why it caused chaos in old codebases. Crazy part? This all happens before your code even runs. JavaScript is wild sometimes 😅 #JavaScript #WebDevelopment #CodingLife #Frontend
To view or add a comment, sign in
-
-
🚀 JavaScript Hoisting Explained (Simply!) Hoisting means JavaScript moves all variable and function declarations to the top of their scope before code execution. If that definition sounds confusing, see this example 👇 console.log(a); var a = 5; Internally, JavaScript actually does this 👇 var a; // declaration is hoisted (moved up) console.log(a); a = 5; // initialization stays in place ✅ Output: undefined --- 🧠 In Short: > Hoisting = JS reads your code twice: 1️⃣ First, to register variables & functions 2️⃣ Then, to execute the code line by line --- 💡 Tip: var → hoisted & initialized as undefined let / const → hoisted but not initialized (stay in Temporal Dead Zone) --- #JavaScript #Hoisting #WebDevelopment #CodingTips #JSInterview #Frontend #React #100DaysOfCode
To view or add a comment, sign in
-
The Simple JS Experiment That Completely Changed How I See Constructors Most JavaScript developers learn constructors one way: create, initialize, return the instance. That’s the story. That’s what everyone teaches. But then recently I realized that story is missing something. Today I’m going to show you why constructors returning functions exist, what they actually enable, and why this weird pattern solves problems that rigid constructors just can’t touch. #javascript #typescript #web https://lnkd.in/d5n3x8TV Let’s get started with the interesting part.
To view or add a comment, sign in
-
“The Secret Behind JavaScript’s Magic — The Event Loop 🧠” When I first learned JavaScript, I used to wonder — how can it handle so many things at once even though it’s single-threaded? 🤔 The answer lies in one beautiful mechanism — The Event Loop. Here’s what actually happens behind the scenes 👇 1️⃣ JavaScript runs in a single thread — only one thing executes at a time. 2️⃣ But when something async happens (like setTimeout, fetch, or Promise), those tasks are offloaded to the browser APIs or Node.js APIs. 3️⃣ Once the main call stack is empty, the event loop takes pending callbacks from the task queue (or microtask queue) and pushes them back into the stack to execute. So while it looks like JavaScript is multitasking, it’s actually just scheduling smartly — never blocking the main thread. Example:- console.log("Start"); setTimeout(() => console.log("Inside Timeout"), 0); Promise.resolve().then(() => console.log("Inside Promise")); console.log("End"); Output:- Start End Inside Promise Inside Timeout Even though setTimeout was “0 ms”, Promises (microtasks) always run before timeouts (macrotasks). That’s the secret sauce 🧠💫 Understanding this single concept can help you debug async behavior like a pro. #JavaScript #EventLoop #Async #WebDevelopment #Coding
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