logo

kamal's Blog

🤟 Welcome to my tech blog. 💻

About Me

Hey there, I’m Kamal! 🤘 A tech enthusiast with a knack for sharing my mostly silly (and occasionally brilliant 🤯) discoveries in the coding world. This blog is my playground, where I spill the beans on what I’m learning—whether it’s the basics, the latest tech, or random tidbits that might make you go, “Wait, that’s a thing?” 😅

Here, you’ll find a mix of flexing, cheeky jokes, and even the occasional “mokka” humor to keep things real. Coding is fun, and I write these posts with that same energy! So if you’re having a rough day, stop by—you’ll either walk away grinning or at least wondering, “What was that?!” 😜

Stick around, laugh a little, learn a lot, and remember: coding is fun, and so is reading about it here! 🎉

JavaScript Hoisting EXPOSED! 😱

The Great Hoisting Drama in JavaScript: A Tale of Variables and Functions 🎭💥

Alright, folks, gather around for today’s showstopper: JavaScript Hoisting! 🎪 You’ve probably heard the term a hundred times, maybe even nodded knowingly in a conversation, all the while secretly thinking, “What the heck is hoisting?” Don’t worry, you’re not alone—I’ve been there too. 😅

But then I decided to dig deep, and let me tell you, it’s not what it seems! The name “hoisting” is kind of misleading, and oh boy, the drama that unfolded when I found out what’s really happening behind the scenes. Buckle up, because this is going to be one wild ride. 🎢


What They Say Hoisting Is

Let’s start with the basics: People will tell you hoisting means that variables and functions are magically lifted to the top of the code during execution. Like some grand ceremony where your code lines up and gets dragged to the top by an invisible crane. 🏗️ Sounds cool, right?

But hold on! JavaScript isn’t some mystical force with construction equipment. It’s all a clever illusion. In reality, there’s no “lifting” happening—just a little trick JavaScript plays during the execution phase.


What Actually Happens 🤔

So, here’s the deal: Before your code runs, JavaScript does a bit of housekeeping in the background. It scans through your code and allocates memory for variables and functions during the creation phase. This is where the so-called “hoisting” comes in.

  • Variables declared with var: Only their declaration (not their value) is stored in memory. That’s why if you try to access a var variable before it’s assigned, you’ll get undefined.

  • Variables declared with let or const: These are also hoisted, but they stay in a "temporal dead zone" (TDZ) until the code execution reaches them. Try accessing them early? Boom—ReferenceError. 💣

  • Functions: Entire function declarations are hoisted! You can call them even before they’re defined. Talk about being a show-off. 🤹‍♂️


A Little Drama in the Code 🎭

Here’s a classic example of hoisting shenanigans:

console.log(myVar); // undefined
var myVar = 42;
console.log(myVar); // 42

You’d think this would throw an error, right? But nope. JavaScript is like, “Relax, I’ve got this.” During the creation phase, it sees var myVar and assigns it a placeholder value of undefined. So, by the time the first console.log runs, it’s ready with undefined instead of crashing your program. Pretty slick, huh? 😎

Now, compare this with let:

console.log(myLet); // ReferenceError: Cannot access 'myLet' before initialization
let myLet = 42;

Why the drama? Well, let and const are too cool to settle for undefined. They’re like, “Touch me before I’m initialized, and it’s game over.” 🛑


So Why Call It Hoisting? 🤷‍♂️

Honestly? They could’ve named it "preparation phase," or something boring like that. But no, someone decided “hoisting” sounded cooler. And let’s face it, it does. The idea of your variables and functions being “hoisted” to the top of the code feels dramatic, almost heroic. 🦸‍♂️

But remember, nothing is actually moved. It’s all just memory allocation during the creation phase. The code stays exactly where you wrote it—JavaScript just pretends otherwise.


My Journey to Understanding Hoisting 🕵️‍♂️

When I first heard about hoisting, I thought, “Oh wow, my code’s just getting rearranged magically? That’s cool!” But the reality hit me harder than a runtime error in production. It’s not magic—it’s JavaScript doing its thing, being its quirky, lovable self. 💕

At first, I was confused. Then annoyed. And then, finally, enlightened. It’s like discovering a plot twist in your favorite show—you hate it at first, but then you realize it’s kind of genius. 🎬


Flexing the Knowledge 💪

Let’s flex a bit. Here’s what you need to remember about hoisting:

  • var is old-school: Hoisted with undefined. Avoid it if you want to stay cool. 😎
  • let and const are the new kids: Hoisted but in a TDZ. Use them for better, safer code. ✅
  • Functions are the drama queens: Entirely hoisted, ready to be called from anywhere in the script. 👑

Moral of the Story

If you’re still trying to wrap your head around hoisting, here’s my advice: Don’t overthink it. Just know that JavaScript is handling your variables and functions in the background before it starts running your code. No cranes. No magic. Just good old memory allocation. 🧠

And if you’re using var in 2024? Please, just stop. Let’s leave that in the past where it belongs. Use let and const like the modern coder you are. 🚀


End Note:

That’s it for today’s hoisting drama! 🎭 If you’re still confused, don’t worry—it took me years to truly get it. But hey, I’ve saved you the pain, so now you can flex your knowledge like a pro. Stay tuned for more fun and chaos in my next post.