💡 Hoisting happens before code execution. ❓ Why are function declarations hoisted but arrow functions are not? In JavaScript, hoisting means the JS engine moves declarations to the top of the file before running the code. ✅ Function declarations are fully hoisted. That means both the function name and its body are stored in memory during the creation phase. So you can call a function declaration even before writing it in the code. ❌ Arrow functions are not hoisted the same way because they are treated like variables. If an arrow function is assigned to const or let, only the variable name is hoisted—not its value. Until the code reaches that line, the arrow function doesn’t exist yet. 👉 So if you try to call an arrow function before it’s defined, JavaScript throws an error. #engineer #webdevelopment #javascript #mernstackdeveloper #fullstackdeveloper #learnwithisnaan
Muhammad Isnaan Ashraf’s Post
More Relevant Posts
-
💡 Everything in JavaScript is an object (almost). ❓ Why is null also an object? When you check the type of null in JavaScript, you’ll see something strange: typeof null; // "object" 😵 But here’s the truth 👉 null is NOT actually an object. 🔹 So why does this happen? This is a historical bug from the early days of JavaScript. Internally, JavaScript used type tags to identify values, and null was given the same tag as objects. Once JavaScript became widely used, this mistake couldn’t be fixed—changing it would break millions of existing programs. 🔹 What is null really? null is a primitive value that means: “No value” “Intentionally empty” “Nothing here” It’s often used to reset variables or show that something is missing on purpose. ⚠️ Why this matters If you rely only on typeof, you can write buggy logic. if (typeof value === "object") { // This will also run for null ❌ } ✅ Best practice Always check for null explicitly: value === null; 💡 Takeaway null showing up as "object" is a JavaScript bug that became a feature. Knowing this helps you avoid confusing bugs and write safer code #learnwithisnaan #JavaScriptTips #ModernJavaScript #ES6 #DeveloperTips #CleanCode #JSDevelopers
To view or add a comment, sign in
-
-
Why JavaScript doesn't crash when you call a function before defining it. 🧠 I recently dove deep into the "Execution Context" of JavaScript, and the concept of Hoisting finally clicked. If you’ve ever wondered why this code works: greet(); function greet() { console.log("Hello LinkedIn!"); } ...the answer lies in how the JS Engine treats your code before it even runs a single line. The Two-Phase Secret: Memory Creation Phase: Before the "Thread of Execution" starts, JavaScript scans your code and allocates memory for variables and functions. Functions are stored in their entirety in the Variable Environment. Variables (var) are stored as undefined. Code Execution Phase: Now, the engine runs the code line-by-line. Because the function is already sitting in the memory component, calling it on line 1 is no problem! The Key Takeaway: Hoisting isn't "moving code to the top" (that’s a common myth). It’s actually the result of the Memory Creation Phase setting aside space for your declarations before execution starts. Understanding the "how" behind the "what" makes debugging so much easier. #JavaScript #WebDevelopment #CodingTips #Hoisting #ProgrammingConcepts
To view or add a comment, sign in
-
-
🚀 JavaScript Magic: Why "Undefined" is actually a Feature, not a Bug! I just had a "Wow" moment diving into the JavaScript Execution Context, and it changed how I look at my code. Ever wondered why you can console.log a variable before you even declare it, and JavaScript doesn't lose its mind? 🤯 🧠 The Secret: Two-Phase Execution When your code runs, JavaScript doesn't just start at line 1. It takes two passes: 1.Memory Creation Phase: JS scans your code and allocates space for all variables and functions. 2. Execution Phase: It runs the code line-by-line. ⚡ The var Behavior (Hoisting) If you use var, JavaScript initializes it as undefined during the memory phase. Result: You can log it early. No error, just a quiet undefined. It’s like the variable is there, but its "suit" hasn't arrived yet. 🛑 The let & const Twist (TDZ) Try the same thing with let or const, and the engine throws a ReferenceError. Why? The Temporal Dead Zone (TDZ). While let and const are also "hoisted," they aren't initialized. They stay in a "dead zone" from the start of the block until the moment the code actually hits the declaration. The Lesson: JavaScript isn't just reading your code; it's preparing for it. Understanding the Execution Context makes debugging feel like having X-ray vision. 🦸♂️ Have you ever been bitten by the Temporal Dead Zone, or do you still find yourself reaching for var out of habit? Let’s discuss! 👇 #JavaScript #WebDevelopment #CodingTips #Frontend #Programming101
To view or add a comment, sign in
-
🗓️ Day 61/100 – Understanding the JavaScript Scope Chain Today I finally understood why sometimes variables work… and sometimes they suddenly don’t. The answer = Scope Chain At first I thought JavaScript just “searches everywhere” for a variable. But no — it actually follows a very specific path. JavaScript looks for variables in this order: 1️⃣ Current function scope 2️⃣ Parent function scope 3️⃣ Global scope It climbs upward step-by-step until it finds the variable. This is called the scope chain. --- Example idea: A function inside a function inside a function… The inner function can access outer variables But the outer function cannot access inner variables So access flows inside → outside Never outside → inside --- Big realization today 💡 Most bugs I faced earlier were not logic mistakes… They were scope mistakes. If you understand scope chain: • Closures make sense • Hoisting becomes clearer • Debugging becomes easier JavaScript stops feeling random — It starts feeling predictable. Slowly the language is revealing its rules, not magic. #100DaysOfCode #JavaScript #Scope #WebDevelopment #Frontend #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Common Types of Errors in JavaScript While coding in JavaScript, you’ve probably seen errors in the console. Understanding them makes debugging much easier. Here are the most common types: 🔹 1. Syntax Error Occurs when the code breaks JavaScript rules. let a = ; // ❌ Missing value The code won’t run until the syntax is fixed. 🔹 2. Reference Error Occurs when trying to use a variable that doesn’t exist. console.log(x); // ❌ x is not defined 🔹 3. Type Error Occurs when an operation is performed on the wrong data type. let num = 10; num.toUpperCase(); // ❌ Not a string 🔹 4. Range Error Occurs when a value is out of the allowed range. let arr = new Array(-1); // ❌ Invalid array length 🔹 5. Logical Error The code runs, but the output is wrong. let total = 10 + "5"; console.log(total); // "105" ❌ Instead of 15 💡 Tip: • Syntax errors stop execution • Runtime errors crash execution • Logical errors give wrong results Understanding errors is the first step to becoming better at debugging. #JavaScript #WebDevelopment #FrontendDevelopment #CodingTips #Debugging
To view or add a comment, sign in
-
💗 Hoisting in JavaScript : In Simple Terms Earlier, I used to wonder: “Where exactly do we use hoisting in real projects?” Later I understood something important - Hoisting is not something we “use.” It’s something that’s already happening. Before running your code, JavaScript scans everything and registers variable and function declarations. Like taking attendance before starting class. That process is called hoisting. Example: console.log(a); //undefined var a = 10; Because internally, JavaScript treats it like: var a; console.log(a); a = 10; Now with let: console.log(b); //This throws an error. let b = 20; Why? Because let and const are hoisted too - but they stay inside something called the "Temporal Dead Zone" until initialization. 💡 Hoisting isn’t a feature we manually use. It’s a mechanism built into JavaScript. Even if we don’t think about it, it’s working behind the scenes every time our code runs. JavaScript isn’t unpredictable. It just follows its own execution rules. #JavaScript #FrontendDevelopment #LearnInPublic #InterviewPrep
To view or add a comment, sign in
-
🚀 JavaScript Tip: var vs let vs const — Explained Simply Understanding how variables work in JavaScript can save you from hard-to-debug issues later. Think of variables as containers that hold values ☕ 🔹 var – Old Style (Not Recommended) ➡️ Function scoped ➡️ Can be re-declared & reassigned ➡️ Gets hoisted → may cause unexpected bugs 👉 Use only if maintaining legacy code 🔹 let – Modern & Safe ➡️ Block scoped {} ➡️ Cannot be re-declared ➡️ Can be reassigned ➡️ Hoisted but protected by Temporal Dead Zone 👉 Best for values that change over time 🔹 const – Locked & Reliable ➡️ Block scoped {} ➡️ Cannot be re-declared or reassigned ➡️ Must be initialized immediately 👉 Best for fixed values and cleaner code ✅ Best Practice Use const by default, switch to let only when reassignment is needed, and avoid var 🚫 💡 Small fundamentals like these make a big difference in writing clean, scalable JavaScript. #JavaScript #WebDevelopment #FrontendDevelopment #ProgrammingTips #LearnJavaScript #CodingBestPractices #DeveloperLearning #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
Execution of code in JavaScript Have you ever thought about how JavaScript runs code behind the scenes. JavaScript executes code in two main phases: 1- Memory Creation Phase [i] Memory is allocated to variables and functions [ii] Variables are initialized with undefined 2- Execution Phase [i] Code runs line by line [ii] Values are assigned [iii] Functions are executed To manage function calls, JavaScript uses the Call Stack, which follows the LIFO (Last In, First Out) principle.
To view or add a comment, sign in
-
-
I once thought extending JavaScript built-in prototypes was a power move. So I did it. I added: • String.prototype.toTitleCase() • String.prototype.toStdNumber() • Number.prototype.toStd() And honestly? It felt amazing. Suddenly I could write: price.toStd(2) instead of formatNumber(price, 2) Cleaner. Shorter. Elegant. ✨ Until I realized what I had actually done… I hadn’t improved my code. I had modified JavaScript itself. That means: ⚠ Every string now had my methods ⚠ Every number everywhere inherited them ⚠ Any library could conflict with them ⚠ Bundle order could break them ⚠ Future JS specs could override them It’s not just a helper. It’s a global mutation. That’s when it hit me: Prototype extension isn’t a shortcut. It’s a hidden global side effect. Senior engineers don’t avoid it because it’s impossible. They avoid it because it’s unpredictable. Now my rule is simple: ✔ Utilities ✔ Pure functions ✔ Explicit imports ❌ Prototype hacks Just because JavaScript lets you do something… doesn’t mean production should. 😉 #JavaScript #CleanCode #Programming #SoftwareEngineering #WebDevelopment
To view or add a comment, sign in
-
-
💡 Sunday Dev Tip: JavaScript Array Methods Stop writing loops. Use array methods instead! ❌ Traditional Loop: let doubled = []; for (let i = 0; i < numbers.length; i++) { doubled.push(numbers[i] * 2); } ✅ Modern Approach: const doubled = numbers.map(n => n * 2); Master These Methods: → .map() - Transform each element → .filter() - Keep elements that match → .reduce() - Calculate single value → .find() - Get first match → .some() / .every() - Test conditions Your code becomes: ✅ More readable ✅ Less error-prone ✅ Easier to maintain ✅ More functional Which array method do you use most? 💬 #JavaScript #CleanCode #WebDevelopment #CodingTips #ES6
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