🧠 JavaScript Hoisting Confusion That Breaks Logic Look at this code: console.log(sum(2, 3)); var sum = function (a, b) { return a + b; }; Most expect it to print 5. Instead, it throws an error. Only declarations are hoisted, not function expressions. Use function declarations when order matters. #JavaScript #Hoisting #JSConcepts #FrontendDevelopment #WebDevelopment #CleanCode #SoftwareEngineering
JavaScript Hoisting Causes Logic Errors
More Relevant Posts
-
Ever tried reading a property that doesn’t exist in JavaScript? No crash. No error. Just… undefined 😄 Most of us do this: if (user.noSuchProperty === undefined) { ... } Works 99% of the time. Until it doesn’t. let obj = { test: undefined }; console.log(obj.test); // undefined console.log("test" in obj); // true ← surprise! The property exists — it just happens to hold undefined. That tiny difference is exactly why the in operator exists. Have you ever been bitten by this gotcha? Or do you still prefer === undefined in everyday code? #JavaScript #CodingTips #WebDevelopment
To view or add a comment, sign in
-
A classic JavaScript trap 🧠 What will this log? const values = [ 1, 1, true, false, 0, '', {}, [] ]; console.log( values.filter( parseInt ) ); console.log( values.filter( Boolean ) ); For the first log, is it [1, 1]? Or something else? What about the second one? Think first. Then run it. The result is not what most people expect 😄 #JavaScript #CodingChallenge #WebDevelopment
To view or add a comment, sign in
-
-
🚀 JavaScript Variables, Finally Decoded: var vs let vs const If you’ve ever wondered when to use var, let, or const, this breakdown makes it crystal clear. 💡 Key takeaways every JS developer should know: var is function-scoped and hoisted as undefined → easy to introduce bugs let and const are block-scoped → safer and more predictable const prevents reassignment (not immutability) let is perfect for loop counters and values that change Redeclaration errors = protection, not punishment 😄 ✅ Modern best practice Default to const Use let only when reassignment is required 🚫 Avoid var in modern JavaScript Cleaner scope. Fewer bugs. Happier future you. #JavaScript #WebDevelopment #Frontend #ProgrammingTips #ES6 #CleanCode #DevLife
To view or add a comment, sign in
-
-
Today I spent time deeply understanding JavaScript closures and it turned out to be really interesting. Here are a few key takeaways: 1. An inner function can access variables from its outer function 2. Even after the outer function has finished execution, the inner function still holds a reference to those variables 3. If closures are not used properly, they can lead to memory leaks and performance issues, especially under heavy load #JavaScript #Closures #FrontendDevelopment #LearningInPublic #WebDevelopment
To view or add a comment, sign in
-
-
Day 42/100 – An Important JavaScript Concept Many People Ignore Not all performance issues come from bad code. Sometimes they come from too many function calls. 📌 Topic: Debouncing vs Throttling Both are used to control how often a function executes. ✅ Debouncing Runs the function only after a certain time has passed since the last event. Example use cases: • Search input • Form validation 👉 Executes when user stops typing. ✅ Throttling Runs the function at a fixed interval, no matter how many times the event occurs. Example use cases: • Scroll events • Window resize 👉 Executes every X milliseconds. Understanding these concepts helps build faster and smoother applications. Small improvements. Big impact. On to Day 43 🚀 #100DaysOfCode #JavaScript #WebDevelopment #LearningInPublic #Consistency #FrontendDevelopment
To view or add a comment, sign in
-
-
🟢 Day 17 / 100 – 📌JavaScript Practice 🚀 ✅ Practiced nested arrays using a Tic-Tac-Toe board today. ✅Explored how indexing works in JavaScript by accessing valid, invalid, and negative indexes through the console. • Learned - Errors and undefined values help in understanding how JavaScript actually behaves under the hood. #Day17 #JavaScript #WebDevelopment #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 What is Proxy & Reflect in JavaScript? 🔹 Proxy – Intercept and customize object operations like get, set, and delete. const user = new Proxy({ name: "Rahul" }, { get(target, prop) { return prop in target ? target[prop] : "Not Found"; } }); console.log(user.name); // Rahul console.log(user.age); // Not Found 🔹 Reflect – Provides built-in methods to perform default object operations safely and cleanly. const user = { name: "Rahul" }; console.log(Reflect.get(user, "name")); // Rahul Reflect.set(user, "age", 25); console.log(user.age); // 25 💡 Quick takeaway: Proxy = Control object behavior Reflect = Perform object operations safely. #JavaScript #WebDevelopment #Frontend #CodingTips #Programming #DevCommunity #LearnJavaScript
To view or add a comment, sign in
-
🤔 Quick question: If JavaScript is so powerful, why is it single-threaded? When I first learned that JavaScript runs on a single thread, my reaction was: “Wouldn’t that make it slow?” Turns out… being single-threaded is actually a design choice, not a limitation 👇 console.log("Start"); while (true) { // imagine a heavy blocking task } console.log("End"); 💡 What’s happening here? JavaScript executes code one task at a time. A blocking operation stops everything else. No other code can run until the current task finishes. This is why JavaScript is called single-threaded. So why would JS be designed this way? - Simpler programming model (no race conditions by default) - Predictable execution order - Perfect fit for the browser (DOM is not thread-safe). Takeaway: JavaScript runs on a single thread, meaning it can do one thing at a time. Asynchronous behavior doesn’t come from multi-threading — it comes from the event loop. #JavaScript #WebDevelopment #FullStack #LearningInPublic
To view or add a comment, sign in
-
Day 6: Closures in JavaScript Closures — one of the most powerful (and confusing) concepts in JavaScript 🤯 A closure is created when a function remembers variables from its outer scope, even after the outer function has finished execution. 🔹 Example function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const fn = outer(); fn(); // 1 fn(); // 2 🔍 What’s happening here? outer() finishes execution But inner() still remembers count This memory + function = closure 🔹 Key Rules ✅ Closures are formed because of lexical scope ✅ Functions retain access to their outer variables ✅ Each function call creates a new closure 🔹 Why are closures important? ✔️ Data hiding & encapsulation ✔️ Used in debouncing, throttling, currying #JavaScript #Closures #ScopeChain #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
We often focus on component optimizations, but the biggest performance win often hides in plain sight: your initial JavaScript bundle size. I’ve seen production applications become orders of magnitude faster just by being ruthless with code splitting. It’s not about shipping less code *overall*, but about not delivering code until it's ABSOLUTELY necessary. Think route-level lazy loading, or even conditionally importing heavy third-party libraries. Audit your critical render path. If a component or dependency isn't needed for the first paint, defer it. Learn more about effective code splitting: https://lnkd.in/gh2A4yGw #FrontendPerformance #WebDevelopment #CodeSplitting #JavaScript
To view or add a comment, sign in
Explore related topics
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