Blog 04 of my JS Unlocked series is live! 🚀 Function Declaration vs Function Expression: What's the Difference? 👇 These two look almost the same — but one works before you define it and the other throws an error. That one difference has caused bugs in every developer's life at least once. In this one I cover: ✅ What functions are and why we need them ✅ Declaration vs Expression — side by side ✅ Hoisting explained simply (no jargon) ✅ When to use which in real projects ✅ Hands-on challenge — call both before defining and observe what happens Would love your feedback if you read it 🙏 🔗 https://lnkd.in/d9yFhfah Thanks to Hitesh Choudhary Sir, Piyush Garg #JavaScript #WebDevelopment #Hashnode #WebDevCohort2026 #LearningInPublic #Frontend #JS
JavaScript Functions: Declaration vs Expression Explained
More Relevant Posts
-
🚀 JavaScript Functions — Explained Functions are the backbone of JavaScript What is a Function? >> A function is a reusable block of code designed to perform a specific task. 1. Function Declaration: A function defined using the function keyword with a name. function greet(name) { return "Hello " + name; } ✔️ Hoisted (can be used before declaration) 2. Function Expression: A function stored inside a variable. const greet = function(name) { return "Hello " + name; }; ❌ Not hoisted like declarations 3. Arrow Function: A shorter syntax for writing functions using =>. const greet = (name) => "Hello " + name; ✔️ Clean and concise ⚠️ Does not have its own this 4. Parameters: Variables listed in the function definition. function add(a, b) { } >> a and b are parameters 5. Arguments: Actual values passed to a function when calling it. add(2, 3); >> 2 and 3 are arguments 6. Return Statement: The return keyword sends a value back from a function and stops execution. function add(a, b) { return a + b; } 7. Callback Function: A function passed as an argument to another function. function greet(name) { console.log("Hello " + name); } function processUser(callback) { callback("Javascript"); } 8. Higher-Order Function: A function that takes another function as an argument or returns a function. >> Examples: map(), filter(), reduce() 9. First-Class Functions: In JavaScript, functions are treated like variables. ✔️ Can be assigned to variables ✔️ Can be passed as arguments ✔️ Can be returned from other functions #JavaScript #WebDevelopment #FrontendDevelopment #Coding #Programming #LearnToCode
To view or add a comment, sign in
-
🔍 JavaScript Quirk: this behaves differently than you think This is one of the most confusing parts of JavaScript 👇 const user = { name: "Avinash", greet: function () { console.log(this.name); } }; user.greet(); // ? ✅ Output: "Avinash" Here, this refers to the object calling the function. Now look at this 👇 const greet = user.greet; greet(); // ? 💥 Output: undefined Why? Because this is no longer bound to user. In this case, this refers to the global object (or undefined in strict mode). Now the tricky part 👇 const user = { name: "Avinash", greet: () => { console.log(this.name); } }; user.greet(); // ? 💥 Output: undefined Why? Arrow functions don’t have their own this. They inherit it from their surrounding scope. 🚨 NEVER use this inside arrow functions for object methods. 💡 Takeaway: ✔ this depends on HOW a function is called ✔ Regular functions → dynamic this ✔ Arrow functions → lexical this ✔ Arrow + this in methods = bug waiting to happen 👉 Master this = fewer hidden bugs 🔁 Save this for later 💬 Comment "this" if it clicked ❤️ Like if this helped #javascript #frontend #webdevelopment #codingtips #js #developer
To view or add a comment, sign in
-
🧠 How JavaScript Handles Memory (Simple Explanation) Most developers write JavaScript daily… but very few understand how memory works behind the scenes. And this is exactly why memory leaks happen. 👀 ✅ 1️⃣ Memory Allocation When you create variables, JavaScript automatically allocates memory: let name = "Angular"; let user = { id: 1 }; let nums = [1, 2, 3]; JavaScript stores them in memory without you doing anything. ✅ 2️⃣ Stack vs Heap (Very Important) 🟢 Stack Memory Stores primitive values: number string boolean null undefined Fast and simple. let a = 10; let b = a; b = 20; console.log(a); // 10 Because stack stores copy of value. 🔵 Heap Memory Stores objects and arrays. let obj1 = { name: "Test" }; let obj2 = obj1; obj2.name = "Frontend Dev"; console.log(obj1.name); // Frontend Dev 😬 Because heap stores reference, not copy. ✅ 3️⃣ Garbage Collection (GC) JavaScript automatically removes unused memory. If a value is not reachable, it gets deleted. Example: let user = { name: "Ali" }; user = null; Now the object becomes unreachable → GC removes it. ⚠️ Common Cause of Memory Leaks 🚨 Unremoved Event Listeners button.addEventListener("click", () => console.log("clicked")); If you never remove it, memory keeps growing. 🎯 Why This Matters (Especially in Angular) Understanding memory helps you: ✔ Avoid memory leaks ✔ Improve performance ✔ Write scalable applications ✔ Handle subscriptions properly (RxJS) 💡 Rule for Angular developers: Always unsubscribe or use async pipe. #JavaScript #Angular #Frontend #WebDevelopment #Performance #Programming
To view or add a comment, sign in
-
-
🧠 Day 6 — Closures in JavaScript (Explained Simply) Closures are one of the most powerful (and frequently asked) concepts in JavaScript — and once you understand them, a lot of things start to click 🔥 --- 🔐 What is a Closure? 👉 A closure is when a function “remembers” variables from its outer scope even after that scope has finished executing. --- 🔍 Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 --- 🧠 What’s happening? inner() still has access to count Even after outer() has finished execution This happens because of lexical scoping --- 🚀 Why Closures Matter ✔ Data privacy (like encapsulation) ✔ Used in callbacks & async code ✔ Foundation of React hooks (useState) ✔ Helps create reusable logic --- ⚠️ Common Pitfall for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } 👉 Output: 3 3 3 ✔ Fix: for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } --- 💡 One-line takeaway: 👉 “A closure remembers its outer scope even after it’s gone.” --- If you’re learning JavaScript fundamentals, closures are a must-know — they show up everywhere. #JavaScript #Closures #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
🚀 JavaScript String Interpolation: Small Feature, Big Impact Ever noticed how JavaScript automatically converts arrays into strings when used inside template literals? That’s interpolation behavior in action—and it can be both helpful and tricky. 💡 Implicit Behavior When you embed an array inside a template string: const items = ['React', 'Node', 'TypeScript']; console.log(`Stack: ${items}`); 👉 Output: Stack: React,Node,TypeScript JavaScript internally calls .toString() on the array, which joins elements with commas by default. ⚠️ The Catch This implicit behavior might not always give you the format you want. It works—but not always professionally or readably. ✅ Better Approach: Explicit Control const technologies = ['React', 'Node', 'TypeScript']; const sentence = `I work with ${technologies.join(', ')} daily.`; 👉 Output: I work with React, Node, TypeScript daily. 🔥 Why This Matters Improves readability of your output Gives you full control over formatting Avoids unexpected results in production code 🧠 Pro Tip Always prefer explicit .join() when formatting arrays in strings—clean code > clever shortcuts. #JavaScript #WebDevelopment #CleanCode #Frontend #reactjs #nodejs #ProgrammingTips
To view or add a comment, sign in
-
-
🚀 Higher-Order Functions in JavaScript — A Must-Know Concept! 💡 What is a Higher-Order Function? A function is called a Higher-Order Function if it: ✔️ Takes another function as an argument ✔️ Returns a function as its result 🔍 Simple Example: function greet(name) { return `Hello, ${name}`; } function processUserInput(callback) { const name = "Jake"; return callback(name); } console.log(processUserInput(greet)); 👉 Here, processUserInput is a Higher-Order Function because it accepts greet as a callback. ⚡ Why are Higher-Order Functions powerful? ✅ Code reusability ✅ Cleaner and more readable code ✅ Helps in functional programming ✅ Widely used in JavaScript methods like: array.map(), array.filter(), array.reduce() 🔥 Interview Twist: function multiplier(factor) { return function (number) { return number * factor; }; } const double = multiplier(2); console.log(double(5)); // ? 🧠 Output: 10 👉 Because multiplier returns another function — classic Higher-Order Function! 🔥 The Important Concept: Closure Even though multiplier execution is finished, the inner function still remembers factor. This is called a closure: "A function remembers variables from its outer scope even after that outer function has finished executing." #JavaScript #WebDevelopment #Frontend #CodingInterview #Developers
To view or add a comment, sign in
-
🚀 Understanding JSX in React — Syntax & Rules Simplified! If you're working with React, JSX is everywhere. But JSX is not HTML—it’s JavaScript with a syntax extension. 💡 What is JSX? JSX (JavaScript XML) lets you write UI like this: const element = <h1>Hello, World!</h1>; 👉 Behind the scenes, React converts this into: React.createElement("h1", null, "Hello, World!"); ⚙️ How JSX works 👉 JSX is compiled into JavaScript 👉 It describes what the UI should look like 👉 React uses it to create Virtual DOM 🧠 Key Rules of JSX (Very Important!) 🔹 1. Return a single parent element // ❌ Wrong return ( <h1>Hello</h1> <p>World</p> ); // ✅ Correct return ( <> <h1>Hello</h1> <p>World</p> </> ); 🔹 2. Use className instead of class <div className="container"></div> 🔹 3. JavaScript inside {} const name = "React"; <h1>Hello {name}</h1> 🔹 4. Self-closing tags <img src="image.png" /> 🔹 5. Inline styles as objects <div style={{ color: "red" }}></div> 🧩 Real-world use cases ✔ Building UI components ✔ Rendering dynamic data ✔ Conditional UI rendering ✔ Mapping lists 🔥 Best Practices (Most developers miss this!) ✅ Keep JSX clean and readable ✅ Extract complex logic outside JSX ✅ Use fragments instead of unnecessary divs ❌ Avoid writing heavy logic inside JSX ⚠️ Common Mistake // ❌ Too much logic inside JSX return <h1>{user.isLoggedIn ? "Welcome" : "Login"}</h1>; 👉 Fine for small cases, but extract logic for complex UI 💬 Pro Insight JSX is not about writing HTML in JS— 👉 It’s about describing UI in a declarative way 📌 Save this post & follow for more deep frontend insights! 📅 Day 6/100 #ReactJS #FrontendDevelopment #JavaScript #JSX #WebDevelopment #SoftwareEngineering #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
The Virtual DOM is often explained as a "performance optimization." It isn't. Rich Harris made this argument in 2018 and it still stands — VDOM adds a layer of work on top of DOM operations, not below them. What it gives you is a programming model: describe the output, not the steps. I wrote a tutorial that builds the entire engine from scratch in 71 lines of vanilla JS. Four functions. Zero dependencies. Every design decision is explicit — no magic. If you've ever wondered what React's reconciler actually does, this is the fastest path to understanding it. https://lnkd.in/dXvCzgXy #javascript #react #webdev #frontend #tutorial
To view or add a comment, sign in
-
💡 𝗘𝘃𝗲𝗿 𝗪𝗼𝗻𝗱𝗲𝗿𝗲𝗱 𝗪𝗵𝘆 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗨𝘀𝗲𝘀 𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸𝘀? ⤵️ Callbacks in JavaScript: Why They Exist 🧠 🔗 𝗥𝗲𝗮𝗱 𝗵𝗲𝗿𝗲: https://lnkd.in/ey9JfCas 𝗧𝗼𝗽𝗶𝗰𝘀 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 ✍🏻: ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⇢ Why JavaScript can't "wait" like other languages ⇢ What callbacks actually are (simple mental model) ⇢ Functions as values — the core JS superpower ⇢ Sync vs Async callbacks explained clearly ⇢ Real-world examples: events, setTimeout, array methods, Node.js ⇢ The async problem: why return values don’t work ⇢ Callback Hell (Pyramid of Doom) — why it happens ⇢ Tradeoffs of callbacks in real applications ⇢ Where callbacks are still used today (React, Node, browser APIs) ⇢ How this leads to Promises & async/await Thanks Hitesh Choudhary Sir & Piyush Garg Sir, and the amazing Chai Aur Code community 🙌 #ChaiAurCode #JavaScript #AsyncProgramming #WebDevelopment #SystemDesign #Programming #Hashnode
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