A promise is just an object with two properties. The word "promise" sounds abstract. The actual thing is surprisingly concrete. When fetch() runs, JavaScript immediately returns an object - before any data has come back. That object has two properties: - value (undefined for now) - onFulfillment (an array of functions to run when value arrives) That's it. A placeholder with a slot for data and a list of what to do when it shows up. When the browser finishes the network request, it fills in value and auto-triggers everything in onFulfillment - passing the value as the argument. You get something immediately so your code can keep moving, and you attach behavior to a future value. That's the whole mechanism behind async JavaScript. Next: .then() - what it's actually doing (hint: not what the name implies). #JavaScript #WebDevelopment #SoftwareEngineering #Frontend
JavaScript Promises Explained in Simple Terms
More Relevant Posts
-
I just published a new open-source library: input-mask. I built it because input masking on the frontend often feels more annoying than it should be, especially in simple projects, forms, landing pages, and workflows where you just want to configure the field and move on. The idea is straightforward: apply input masks using HTML attributes only, without needing to write JavaScript just to initialize everything. You add the library via CDN, use attributes like data-mask="pattern" or data-mask="currency" on the input, and it handles the rest. Under the hood it uses IMask, but the whole point was to hide that complexity and make the implementation much more accessible. The first public version already supports: • pattern • regex • number • currency • date Repo: https://lnkd.in/e6pkj7wB CDN: https://lnkd.in/ebc7fdr5 If anyone wants to try it, share feedback, or suggest improvements, I’d love to hear it. #javascript #frontend #webdev #opensource #forms #nocode
To view or add a comment, sign in
-
Stop using Date in JavaScript for new projects The Date object is now officially legacy. TC39 is replacing it with the Temporal API. If you're still writing new Date() in 2026, you're shipping technical debt. Why Date is a problem: Mutable by default: Calling setDate changes the original object. That’s how you get surprise bugs in production. Months start at zero: January is 0. December is 11. Every dev has been burned by this at least once. Timezone chaos: new Date("2026-01-01") can give you Dec 31st at night depending on where your server runs. One object for everything: No way to represent just a date or just a time. It’s always a full datetime whether you want it or not. How Temporal fixes this: It’s immutable, so every operation returns a new value. Months start at 1 like normal humans expect. And you get specific types: PlainDate for dates only, PlainTime for time only, ZonedDateTime when you care about timezones, Duration for math. Instead of adding 86400000 milliseconds to add a day, you just write add one day. Way cleaner. What to do today: For new projects, use date-fns or Day.js until Temporal lands in browsers. If you want to be future-proof, try the @js-temporal/polyfill now. Temporal is Stage 3 and landing soon. Don’t let legacy APIs hold your codebase back. What are you using for dates in 2026? Let me know below 👇 #JavaScript #WebDev #Temporal #CleanCode #Frontend #TypeScript
To view or add a comment, sign in
-
❓ Quick question: Everything works fine… until you move your variable inside a function or block 💥 Why does JavaScript behave like this? 🤔 👉 What’s actually going on here? 🚀 Let’s decode one of the most confusing JavaScript concepts: Scope (Global, Function, Block) 🌍 1. Global Scope (var, let, const) If a variable is declared outside everything → it becomes globally accessible ✔ Works everywhere: • inside { } • inside if • inside loops • inside functions 💡 That’s why this runs smoothly everywhere. 🧠 2. Function Scope Variables declared inside a function are locked 🔒 inside it. 👉 Outside = ❌ Not accessible 👉 Inside = ✅ Works perfectly 💡 Key Insight: Function creates its own private world 📦 3. Block Scope (let & const) let and const are block scoped That means: 👉 Only accessible inside { } Outside the block → 💥 ReferenceError ⚠️ But here’s the twist… var is NOT block scoped ❗ 👉 It ignores { } blocks 👉 Gets hoisted to function/global scope So: Inside block → defined Outside block → still accessible 😲 🔥 Why this matters (real dev insight) Understanding scope helps you: - Avoid accidental bugs 🐛 - Write predictable code 🧠 - Prevent variable leaks ⚠️ - Debug faster 🚀 💡 Final Thought: Most beginners think: 👉 “Scope is simple” But in real-world apps: 👉 Scope mistakes = silent bugs + messy code 📌 Follow along — I’ll keep breaking down JavaScript concepts in a simple way. #JavaScript #FrontendDevelopment #WebDevelopment #CodingTips #LearnJavaScript #Scope #100DaysOfCode #BuildInPublic
To view or add a comment, sign in
-
-
What will happen if you call a variable before initialization? 🤔 That is called Hoisting 👉 "JavaScript moves declarations to the top of their scope before execution" Sounds confusing? Let’s break it down 👇 When you create variables or functions, JavaScript runs your code in 2 phases: 1️⃣ Memory Creation Phase Before execution, JavaScript scans your code and allocates memory Example (mentally): var a → undefined let b → uninitialized (Temporal Dead Zone) 2️⃣ Execution Phase Now JavaScript runs your code line by line 👉 If you access variables before initialization: var → returns undefined let / const → ReferenceError Why does this happen? Because: var is initialized with undefined in memory let and const are hoisted but stay in the Temporal Dead Zone (TDZ) until the line where they are declared Simple way to remember: var => “exist, but don’t have a value yet” let / const => “Don’t touch before declaration” ⚡ Bonus: Function declarations are fully hoisted, so you can call them before defining them Curious how functions behave in hoisting? 🤔 Go Google function vs function expression in JavaScript — it’ll surprise you 👀 That’s hoisting in JavaScript 🚀 #javascript #webdevelopment #coding #frontend #learninpublic #hoisting
To view or add a comment, sign in
-
-
Ever written JavaScript that *shouldn’t* work… but somehow does? 🤯 I remember staring at my screen thinking, “Did JS just ignore my logic?” Here’s the problem 👇 JavaScript has this hidden behavior called **hoisting**. It moves declarations (not values) to the top of the scope *before execution*. Sounds helpful… until it silently breaks your expectations. Here’s the insight 💡 * `var` gets hoisted and initialized as `undefined` * `let` & `const` are hoisted but stay in a “temporal dead zone” 🚫 * Functions? Fully hoisted — you can call them before defining! Example: ```js console.log(a); // undefined 😵 var a = 5; ``` Lesson learned 📌 JavaScript isn’t “weird” — it just follows rules we often ignore. Understanding hoisting = fewer bugs + cleaner logic. Now I always think: “What does JS see *before* running this?” 👀 If you’re diving deeper into JS concepts, I share more here 👉 https://webdevlab.org 🚀 Curious… have you ever been surprised by hoisting in your code? 😄 #JavaScript #WebDevelopment #Frontend #CodingTips #DevLife
To view or add a comment, sign in
-
-
⚡ Day 7 — JavaScript Event Loop (Explained Simply) Ever wondered how JavaScript handles async tasks while being single-threaded? 🤔 That’s where the Event Loop comes in. --- 🧠 What is the Event Loop? 👉 The Event Loop manages execution of code, async tasks, and callbacks. --- 🔄 How it works: 1. Call Stack → Executes synchronous code 2. Web APIs → Handle async tasks (setTimeout, fetch, etc.) 3. Callback Queue / Microtask Queue → Stores callbacks 4. Event Loop → Moves tasks to the stack when it’s empty --- 🔍 Example: console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); --- 📌 Output: Start End Promise Timeout --- 🧠 Why? 👉 Microtasks (Promises) run before macrotasks (setTimeout) --- 🔥 One-line takeaway: 👉 “Event Loop decides what runs next in async JavaScript.” --- If you're learning async JS, understanding this will change how you debug forever. #JavaScript #EventLoop #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
🤯 This made me pause and appreciate how powerful JavaScript really is behind the scenes… Take a look at this 👇 ----------------------------------^^^^^^^^---------------------------------- console.log("Start"); async function test() { try { console.log("Inside Function"); await Promise.reject(); console.log("After Await"); } catch (e) { console.log("Error"); } } test(); console.log("End"); ----------------------------------^^^^^^^^---------------------------------- 🧠 Question: What will be the output? Drop your answer in the comments 👇 What’s fascinating here is how async/await, the call stack, and the microtask queue work together. Even a simple snippet like this can reveal how JavaScript handles execution flow, errors, and asynchronous behavior. 💡 Hint: await pauses the function… But does it block the main thread? And when does the catch actually run? I love these small snippets that challenge how we think JS works vs how it actually behaves. Let’s see who gets it right 👀
To view or add a comment, sign in
-
🔍 JavaScript Bug You Might Have Seen (null vs undefined) You write this: let a; console.log(a); // ? let b = null; console.log(b); // ? Both look similar… But they are NOT the same. 💥 Output: undefined null Now here’s where it gets confusing 👇 console.log(null == undefined); // ? console.log(null === undefined); // ? 💥 Output: true false Wait… WHAT? 🤯 This happens because of how JavaScript treats them. 📌 What is undefined? 👉 A variable that is declared but NOT assigned a value 📌 What is null? 👉 A value that represents “intentional absence of value” (You explicitly set it) 📌 Then why this? null == undefined // true Because == does loose comparison 👉 It treats them as equal BUT: null === undefined // false Because === checks: ✔ Value ✔ Type 💡 Takeaway: ✔ undefined → JS gives it ✔ null → YOU assign it ✔ == → treats them equal ✔ === → they are different 👉 Use undefined for default/unassigned 👉 Use null when you intentionally want “no value” 🔁 Save this before it confuses you again 💬 Comment “null” if this clicked ❤️ Like for more JavaScript deep dives #javascript #frontend #codingtips #webdevelopment #js #developer
To view or add a comment, sign in
-
My form was getting harder to manage… as it grew 😅 Yes, seriously. At first, everything was simple. But as inputs increased, handling state became messy. 💡 I was doing this: const [value, setValue] = useState(""); <input value={value} onChange={(e) => setValue(e.target.value)} /> 👉 Controlled input ⚠️ Problem: • Too many states • More re-renders • Hard to manage large forms 💡 Then I explored another approach: 👉 Uncontrolled components 🧠 Example: const inputRef = useRef(); 👉 Access value when needed 💡 Difference: Controlled → React manages state Uncontrolled → DOM manages state ✅ Result: • Better flexibility • Cleaner logic (for some cases) • Easier form handling 🔥 What I learned: Not every input needs to be controlled. #ReactJS #FrontendDeveloper #JavaScript #CodingTips #WebDevelopment
To view or add a comment, sign in
-
🔍 JavaScript Behavior You Might Have Seen (Prototype Chain) You write this: const arr = [1, 2, 3]; console.log(arr.toString()); // ? 👉 Did you define toString on this array? 👉 Did you even define it anywhere? No. Still it works. Now think step by step 👇 When you do: 👉 arr.toString JavaScript checks: Inside arr → ❌ not found Inside Array.prototype → ❌ not found Inside Object.prototype → ✔ found This step-by-step lookup is called the Prototype Chain 📌 What is Prototype Chain? 👉 It’s the process JavaScript uses to find a property by searching up through prototypes. 📌 How it works? Every object is linked to another object (its prototype) So lookup happens like this: 👉 arr → Array.prototype → Object.prototype → null Same with strings 👇 const str = "hello"; console.log(str.hasOwnProperty("length")); // ? 👉 hasOwnProperty is not in string JS finds it here: 👉 str → String.prototype → Object.prototype 📌 Important point: 👉 JavaScript keeps searching until it finds the property or reaches null 💡 Takeaway: ✔ Prototype Chain = lookup mechanism ✔ JS searches step by step ✔ That’s how all built-in methods work 👉 If you understand this, you’ll understand how JavaScript really works 💬 Comment “chain” if this clicked 🔁 Save this for later ❤️ Like for more JavaScript deep dives #javascript #frontend #codingtips #webdevelopment #js #developer
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