🔥 Regular Function vs Arrow Function in JavaScript — what's the real difference? This confused me when I started. Here's everything you need to know 👇 ━━━━━━━━━━━━━━━━━━━━━━━━━━ 1. The syntax is different Regular functions use the function keyword. Arrow functions use a shorter ( ) => syntax — less typing, cleaner code. 2. The "this" keyword behaves differently This is the BIG one. 📌 Regular function → has its own this. It changes depending on HOW the function is called. 📌 Arrow function → does NOT have its own this. It borrows this from the surrounding code. That's why arrow functions are great inside classes and callbacks — they don't lose track of this. 3. Arrow functions can't be used as constructors You can do new regularFunction() — works fine. You can NOT do new arrowFunction() — it throws an error. 4. Arguments object Regular functions have a built-in arguments object to access all passed values. Arrow functions don't — use rest parameters (...args) instead. ━━━━━━━━━━━━━━━━━━━━━━━━━━ ✅ When to use which? → Use arrow functions for short callbacks, array methods (.map, .filter), and when you need to keep this from the outer scope. → Use regular functions when you need your own this, use as a constructor, or need the arguments object. Understanding this = leveling up as a JavaScript developer 🚀 #JavaScript #WebDevelopment #100DaysOfCode #Frontend #CodingTips #Programming
JavaScript Function Types: Regular vs Arrow Functions
More Relevant Posts
-
🧠 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
-
🚀 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 Functions in JavaScript – The Core of Clean Code Functions are the backbone of JavaScript. They allow you to write reusable, modular, and maintainable code—something every developer should master. 🔹 What is a Function? A function is a block of code designed to perform a specific task, executed when “called” or “invoked.” function greet(name) { return "Hello, " + name + "!"; } console.log(greet("Alex")); 🔹 Types of Functions in JavaScript ✅ Function Declaration Defined using the function keyword and hoisted. function add(a, b) { return a + b; } ✅ Function Expression Stored in a variable, not hoisted. const add = function(a, b) { return a + b; }; ✅ Arrow Functions (ES6) Shorter syntax, great for concise logic. const add = (a, b) => a + b; ✅ Anonymous Functions Functions without a name, often used as callbacks. setTimeout(function() { console.log("Executed!"); }, 1000); ✅ Higher-Order Functions Functions that take or return other functions. const nums = [1, 2, 3]; const doubled = nums.map(n => n * 2); 🔹 Why Functions Matter ✔ Code reusability ✔ Better readability ✔ Easier debugging ✔ Modular programming 💡 Pro Tip: Write small, focused functions. One function = one responsibility. Mastering functions is your first step toward writing scalable and professional JavaScript code. #JavaScript #WebDevelopment #Coding #Programming #Frontend #Developers
To view or add a comment, sign in
-
🧠 Understanding the “this” Keyword in JavaScript (Simple Explanation) The this keyword is one of the most confusing parts of JavaScript. Early on, I used to assume this always refers to the current function — but that’s not actually true. 👉 The value of this depends on how a function is called, not where it is written. Let’s break it down 👇 🔹 1. Global Context console.log(this); In browsers, this refers to the window object. 🔹 2. Inside a Regular Function function show() { console.log(this); } Here, this depends on how the function is invoked. 🔹 3. Inside an Object Method const user = { name: "John", greet() { console.log(this.name); } }; user.greet(); // "John" Here, this refers to the object calling the method. 🔹 4. Arrow Functions Arrow functions do NOT have their own this. They inherit this from the surrounding (lexical) scope. 🔹 5. call, apply, bind These methods allow you to manually control what this refers to. 💡 One thing I’ve learned: Understanding this becomes much easier when you focus on how the function is called, not where it is defined. Curious to hear from other developers 👇 What part of JavaScript confused you the most when you were learning? #javascript #frontenddevelopment #webdevelopment #reactjs #softwareengineering #developers
To view or add a comment, sign in
-
-
🚀 Just published my latest blog on Template Literals in JavaScript! Tired of messy string concatenation using +? Learn how template literals make your code cleaner, readable, and modern 💡 ✅ Real-world examples ✅ Before vs After comparisons ✅ Practical use cases Perfect for beginners in web development 👨💻 🔗 Read here: https://lnkd.in/gJ6qP-ch #JavaScript #WebDevelopment #Coding #Frontend #100DaysOfCode
To view or add a comment, sign in
-
🚀 JavaScript Event Loop Explained (Step-by-Step) JavaScript is single-threaded, meaning it executes one task at a time. So how does it handle asynchronous operations like Promises and setTimeout? Let’s break it down in a simple way: 🔹 Step 1: Synchronous Code (Call Stack) All synchronous code runs first in the Call Stack. Example: console.log("Hello") → executes immediately 🔹 Step 2: Promises (Microtask Queue) When a Promise resolves, its ".then()" callback is added to the Microtask Queue. This queue has higher priority than others. 🔹 Step 3: setTimeout (Callback Queue) setTimeout is handled by Web APIs and, after the timer completes, its callback moves to the Callback Queue. 🔹 Step 4: Event Loop The Event Loop continuously checks: • Is the Call Stack empty? • If yes, execute tasks from queues ⚡ Key Rule: Microtask Queue (Promises) executes before Callback Queue (setTimeout) 💡 Example: console.log("Hello"); Promise.resolve().then(() => console.log("Promise")); setTimeout(() => console.log("Callback"), 0); 📌 Output: Hello Promise Callback Even with 0ms delay, setTimeout runs last because it waits in the Callback Queue. --- 🎯 Why this matters: • Better understanding of async behavior • Easier debugging • Stronger interview preparation 🔖 Save this for future reference #JavaScript #EventLoop #MERN #WebDevelopment #Frontend #NodeJS
To view or add a comment, sign in
-
-
🧠 Day 13 — Class vs Prototype in JavaScript (Simplified) JavaScript has both Classes and Prototypes — but are they different? 🤔 --- 🔍 The Truth 👉 JavaScript is prototype-based 👉 class is just syntactic sugar over prototypes --- 📌 Using Class (Modern JS) class Person { constructor(name) { this.name = name; } greet() { console.log(`Hello ${this.name}`); } } const user = new Person("John"); user.greet(); // Hello John --- 📌 Using Prototype (Core JS) function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log(`Hello ${this.name}`); }; const user = new Person("John"); user.greet(); // Hello John --- 🧠 What’s happening? 👉 Both approaches: Create objects Share methods via prototype Work almost the same under the hood --- ⚖️ Key Difference ✔ Class → Cleaner, easier syntax ✔ Prototype → Core JavaScript mechanism --- 🚀 Why it matters ✔ Helps you understand how JS works internally ✔ Useful in interviews ✔ Makes debugging easier --- 💡 One-line takeaway: 👉 “Classes are just a cleaner way to work with prototypes.” --- #JavaScript #Prototypes #Classes #WebDevelopment #Frontend #100DaysOfCode
To view or add a comment, sign in
-
💡 Short Circuiting in JavaScript — Explained Simply In JavaScript, I came across a powerful concept called Short Circuiting. 👉 In simple words: JavaScript stops checking further values as soon as the result is decided. 🔹 OR (||) Operator It returns the first TRUE (truthy) value it finds. Example: console.log(false || "Javascript"); ✔️ Output: "Javascript" Another example: console.log(false || 10 || 7 || 15); ✔️ Output: 10 👉 Why? Because JavaScript finds 10 (true value) and stops checking further values 🔹 My Practice Code 👨💻 console.log("Short circuit in OR operator"); console.log(false || "Javascript"); console.log(false || "OR Operator"); console.log(false || 3); console.log(false || 10 || 7 || 15 || 20); // short circuit in OR operator console.log(false || "Javascript - client side scripting language" || "HTML - structure of web page" || "CSS - Cascading Style Sheet" || "Bootstrap - Framework of CSS"); 🔹 Recap: ✔️ JavaScript returns the first truthy value ✔️ It does not check remaining values once result is found ✔️ This makes code faster and efficient 📌 In simple words: Short Circuiting = JavaScript stops early when result is found #JavaScript #WebDevelopment #Coding #Learning #Frontend #100DaysOfCode
To view or add a comment, sign in
-
🤯 Why does JavaScript sometimes feel so confusing? Honestly, this doesn’t just happen to beginners. Even experienced developers get surprised by JavaScript from time to time 👨💻 Take this simple example 👇 "11" + 1 // "111" "11" - 1 // 10 At first glance, both lines look almost the same. But JavaScript treats them differently. 👉 In the first line, it joins the values as text, so the result becomes ""111"". 👉 In the second line, it converts the value into a number, so the result becomes "10". Same input. Different result. And that’s exactly where the confusion starts 😅 Here’s another common one 👇 0 == "0" // true 0 === "0" // false 👉 "==" checks only the value 👉 "===" checks both the value and the type That’s why most developers prefer "===" ✅ And this one surprises a lot of people 👇 Boolean("0") // true Boolean([]) // true Boolean(0) // false 👉 The string ""0"" is true 👉 An empty array is also true 👉 But the number "0" is false JavaScript is incredibly powerful 💻 But sometimes it’s so flexible that it creates bugs in places you least expect 🐛 A simple rule I follow: ✔ Always use "===" ✔ Convert types explicitly ✔ Don’t rely too much on automatic conversions ✔ Use entity["software","TypeScript"] in larger projects for safer code JavaScript isn’t broken. Sometimes it just tries to be a little too smart 😄 💬 What’s the most confusing JavaScript behavior you’ve ever come across? Drop it in the comments 👇 🔖 Save this post for your next debugging session 🤝 Share it with a developer friend who needs this #JavaScript #WebDevelopment #Programming #Frontend #Developer #CodingLife
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