Today I learned about Functions in JavaScript! A Function is a reusable block of code designed to perform a specific task. It executes when it is "invoked" or called, helping developers follow the DRY (Don't Repeat Yourself) principle. There are four key types of functions in JavaScript: 1. Function Declaration These are hoisted, meaning they can be called before they are defined in the code. ex. console.log(greet("Vaseem")); function greet(name) { return `Hello, ${name}!`; } 2. Function Expression: A function assigned to a variable. Unlike declarations, these are not hoisted. ex. const add = function(a, b) { return a + b; }; 3. Arrow Functions (ES6+) A concise syntax introduced in modern JavaScript. They do not have their own this binding, making them ideal for callbacks. ex. const multiply = (x, y) => x * y; 4. Immediately Invoked Function Expression (IIFE) A function that runs as soon as it is defined. It is commonly used to create a private scope. ex. (function() { console.log("Programming started.."); })(); #JavaScript #WebDevelopment #Programming #CodingTips #SoftwareEngineering #Frontend #JSFunctions #TechLearning
JavaScript Functions: Types and Examples
More Relevant Posts
-
I thought I understood this JavaScript concept… until I really did 👇 📌 Parameter Scope in JavaScript Function parameters are not special variables they are simply local variables scoped to the function. function greet(userName) { console.log(userName); } console.log(userName); // ❌ ReferenceError: userName is not defined Key Takeaway: userName exists only inside the function's execution context. But here’s the interesting part 👀 Parameters also follow lexical scope, which means inner functions can access them via closures: function outer(x) { function inner() { console.log(x); // ✅ Accesses 'x' from the outer scope } inner(); } And a subtle gotcha most beginners miss ⤵️ Default parameters are evaluated in their own scope at the moment the function is called, strictly before the function body begins to run. Understanding scope like this changed how I read and debug JavaScript code. Small concepts. Big clarity. 🚀 #JavaScript #WebDevelopment #LearningInPublic #Frontend #CodingTips #Scope
To view or add a comment, sign in
-
JavaScript Scope Chain — Explained Simply (No Fluff) If you’ve ever wondered “Where the hell did this variable come from?”, this is for you. Understanding the scope chain explains: Why inner functions can access outer variables Why undefined or ReferenceError happens How JavaScript actually resolves variables ❌ Confusing without scope chain let x = 50; function outerFunction() { function innerFunction() { console.log(x); // Where does x come from? } innerFunction(); } outerFunction(); // 50 ✅ Clear when you know the rule let x = 10; function outerFunction() { let y = 20; function innerFunction() { console.log(x, y); } innerFunction(); } outerFunction(); // 10 20 🔍 How JavaScript finds variables 1️⃣ Look in the current scope 2️⃣ Move to the parent scope 3️⃣ Continue up to the global scope 4️⃣ Not found? → ReferenceError Key takeaway: Inner functions don’t magically get variables — JavaScript walks up the scope chain until it finds them. If you don’t understand scope, you’ll write unpredictable JS. If you do, debugging becomes boring — and that’s a good thing. #JavaScript #WebDevelopment #Frontend #ReactJS #Programming #ScopeChain #CleanCode
To view or add a comment, sign in
-
🚀 Stop Guessing How JavaScript Works: The Event Loop Explained Ever wondered why JavaScript is "single-threaded" but can still handle thousands of concurrent tasks without breaking a sweat? The secret isn't magic; it's the Event Loop. 🎡 If you want to master asynchronous programming, you have to understand how these four pieces play together: 1. The Call Stack 📚 This is where the engine tracks what function is currently running. It’s a LIFO (Last In, First Out) structure. If the stack is busy, nothing else happens. 2. Web APIs 🌐 When you call a setTimeout, a fetch request, or a DOM event, JavaScript "hands off" these tasks to the browser (or Node.js). This keeps the main thread free. 3. The Callback Queue (Task Queue) 📥 Once a Web API finishes its job, the callback (the code you want to run) moves here to wait for its turn. 4. The Event Loop 🔄 The "Gatekeeper." It has one simple job: Look at the Call Stack. If the Stack is empty, take the first task from the Queue and push it onto the Stack. 💡 Why does this matter? Have you ever seen a UI freeze during a heavy calculation? That’s because the Call Stack is clogged, and the Event Loop can't push the "render" or "click" tasks from the queue. Pro Tip: Always remember that Microtasks (like Promises) have a "VIP pass." They get executed before the standard Macrotasks (like setTimeout), even if the timer has already expired! #JavaScript #WebDevelopment #ProgrammingTips #Frontend #SoftwareEngineering #EventLoop
To view or add a comment, sign in
-
🧩 JavaScript – Functions A function is simply a block of code that you can reuse whenever you need it. In simple words: A function is a small machine that does one job when you call it. Example: *** function greet() { console.log("Hello!"); } greet(); greet(); *** Here: ● function greet() → we create the function ● greet() → we call the function Every time you call it, the same code runs again. Why functions are powerful: • They reduce repeated code • They make your program clean • They break big problems into small parts • They are easy to test and debug Think like this: Instead of writing the same logic again and again, you write it once inside a function and reuse it anywhere. Example: *** function add(a, b) { return a + b; } add(2, 3); // 5 add(10, 20); // 30 *** One function. Many uses. Functions are the building blocks of JavaScript. Mastering them makes you think like a real developer. #Day2 #JavaScript #Functions #Frontend #WebDevelopment #LearningInPublic #Developers #CareerGrowth
To view or add a comment, sign in
-
Most beginners don’t hate JavaScript… They hate callbacks 😐 Because once your app grows, your code starts looking like this 👇 Nested callbacks. Unreadable logic. Debugging nightmare. This problem even has a name 👉 Callback Hell 🔥 That’s exactly why JavaScript introduced PROMISES. Promises didn’t change async behavior. They changed how humans read async code. ✔️ No deep nesting ✔️ Clear execution flow ✔️ One place for error handling I explained this step-by-step with visuals and real code examples in 👉 JavaScript Confusion Series – Part 2 🔗 Read here: https://lnkd.in/gdxzCMEB If callbacks ever made you think “I understand JS… but something still feels off” 👉 this will finally make it CLICK 💡 💬 Comment “NEXT” if you want Part 3: Why async/await feels like magic 🔥 #JavaScript #WebDevelopment #FrontendDevelopment #LearnJavaScript #JavaScriptConfusionSeries #Programming #CodeNewbie
To view or add a comment, sign in
-
Ever wondered how objects in JavaScript use features they never created? 🤔 From where do the methods and properties of objects actually come? They come from something called Prototype ✨ Think of it like this: You don’t own a pen 🖊️ But your friend does. When you need it, you borrow it 🤝 JavaScript works the same way. If an object doesn’t have something, it borrows it from its prototype. 🧱 Object Example const person = { name: "Bushra" }; console.log(person.hasOwnProperty("name")); // true We never wrote hasOwnProperty. So where did it come from? It comes from Object.prototype. JavaScript searches like this: person → Object.prototype → null 📦 Array Example const numbers = [1, 2, 3]; numbers.push(4); numbers.pop(); We never created push or pop. They come from Array.prototype. JavaScript searches like this: numbers → Array.prototype → Object.prototype → null #JavaScript #LearnJS #WebDevelopment #Frontend #CodingJourney #Programming #TechLearning #DeveloperLife #100DaysOfCode #JSBasics
To view or add a comment, sign in
-
The only JavaScript function that comes with a "Pause" button. ⏯️ In JavaScript, the golden rule of functions is usually "Run-to-Completion." Once a function starts, it doesn't stop until it returns or finishes. 𝐄𝐧𝐭𝐞𝐫 𝐭𝐡𝐞 𝐆𝐞𝐧𝐞𝐫𝐚𝐭𝐨𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧. 🤯 It completely breaks the rule. It is a special type of function that can be paused in the middle of execution and resumed later from exactly where it left off. 𝐓𝐡𝐞 𝐌𝐚𝐠𝐢𝐜 𝐒𝐲𝐧𝐭𝐚𝐱: 1️⃣ `function*`: The asterisk tells JS this isn't a normal function. 2️⃣ `yield`: This keyword acts like a pause button. It spits out a value and freezes the function's state. 3️⃣ `.next()`: This is the play button. Call it to resume the function until it hits the next `yield`. 𝐖𝐡𝐲 𝐢𝐬 𝐭𝐡𝐢𝐬 𝐮𝐬𝐞𝐟𝐮𝐥? (𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧) Generators allow for 𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧. You don't have to calculate a list of 1 million items at once (crashing your memory). You can generate them one by one, only when you actually need them. It’s perfect for infinite streams, ID generators, or defining complex state machines. Check out the syntax breakdown below! 👇 Have you used Generators in production (maybe with Redux Saga)? #JavaScript #WebDevelopment #CodingPatterns #AdvancedJS #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
⚡ JavaScript Event Loop — The Concept That Makes JS Feel “Fast.” Ever wondered how JavaScript handles multiple tasks even though it’s single-threaded? Here are the key things to understand: 🧩 Call Stack Runs your code line by line (one task at a time). 🌐 Web APIs (Browser) Handles slow tasks like setTimeout, fetch, DOM events, etc. 📥 Callback Queue (Task Queue) Stores callbacks waiting to run after the stack is empty. ⚡ Job Queue (Microtask Queue) Promises go here — and it runs before the callback queue ✅ 🔁 Event Loop Continuously checks if the call stack is empty, then pushes queued tasks back to execution. Understanding this helps you: ✅ predict async output order ✅ fix “why is this logging first?” confusion ✅ write better Promise/async-await code ✅ understand sequence vs parallel vs race I wrote a beginner-friendly breakdown with examples. Link in the comments 👇 #JavaScript #WebDevelopment #Frontend #Programming #LearnJavaScript #SoftwareEngineering #Async #EventLoop
To view or add a comment, sign in
-
-
💠 JavaScript slice() Method — Explained Simply The slice() method is used to extract a portion of an array or string without modifying the original data. It returns a new array or string, making it a non-mutating and safe operation. 🔍 Key Characteristics 🔸 Does not mutate the original array or string 🔸 Supports negative indexes 🔸 Commonly used for copying arrays, pagination, and sub-list creation 👉 Real-World Use Case 🔹 In React applications, slice() is often used for: 🔹 Pagination 🔹 Displaying partial lists 🔹 Maintaining immutability during state updates 💡 Why it matters 🔹 In React and modern JavaScript, immutability is key. 🔹 slice() helps maintain clean, predictable state updates. #JavaScript #FrontendDevelopment #ReactJS #WebDevelopment #CodingTips #LearnJavaScript #Programming
To view or add a comment, sign in
-
-
🤔 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗤𝘂𝗶𝗿𝗸 𝗔𝗹𝗲𝗿𝘁: 𝗪𝗵𝘆 𝗱𝗼𝗲𝘀 𝘵𝘺𝘱𝘦𝘰𝘧 𝘯𝘶𝘭𝘭 === "𝘰𝘣𝘫𝘦𝘤𝘵"? Ever wondered why JavaScript thinks null is an object? It's not a feature—it's a bug from 1995 that we're stuck with forever! 𝗧𝗵𝗲 𝗢𝗿𝗶𝗴𝗶𝗻 𝗦𝘁𝗼𝗿𝘆: In JS v1, values had type tags. Objects got 000, and null (the NULL pointer) was all zeros. Boom—misidentified as an object. 𝗪𝗵𝘆 𝗖𝗮𝗻'𝘁 𝗪𝗲 𝗙𝗶𝘅 𝗜𝘁? 𝗦𝗶𝗺𝗽𝗹𝗲: backwards compatibility. In 2011, they tried fixing it by making 𝘵𝘺𝘱𝘦𝘰𝘧 𝘯𝘶𝘭𝘭 === "𝘯𝘶𝘭𝘭". 𝗥𝗲𝘀𝘂𝗹𝘁? Mass breakage across the web. 🔥 𝗠𝗶𝗹𝗹𝗶𝗼𝗻𝘀 𝗼𝗳 𝗰𝗼𝗱𝗲𝗯𝗮𝘀𝗲𝘀 𝗵𝗮𝘃𝗲: 𝘫𝘢𝘷𝘢𝘴𝘤𝘳𝘪𝘱𝘵𝘪𝘧 (𝘵𝘺𝘱𝘦𝘰𝘧 𝘷𝘢𝘭𝘶𝘦 === "𝘰𝘣𝘫𝘦𝘤𝘵" && 𝘷𝘢𝘭𝘶𝘦 !== 𝘯𝘶𝘭𝘭) { // 𝘩𝘢𝘯𝘥𝘭𝘦 𝘰𝘣𝘫𝘦𝘤𝘵𝘴 } Remove that && 𝘷𝘢𝘭𝘶𝘦 !== 𝘯𝘶𝘭𝘭 check, and chaos ensues. 𝗧𝗵𝗲 𝗟𝗲𝘀𝘀𝗼𝗻: Sometimes, the web's greatest strength (never breaking old sites) is also its biggest constraint. We live with legacy bugs because stability > theoretical perfection. What's your favorite JavaScript quirk? 👇 #JavaScript #WebDevelopment #Programming #DeveloperLife #TechTrivia #TechInnovation
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