✅ Logical Operators in JavaScript – Complete Guide with Real Examples

Image
πŸ“Œ Introduction Logical operators help you combine conditions, control flow, and make decisions in JavaScript. In this guide, you’ll learn how to use && , || , and ! effectively with examples and real-world use cases. πŸ” What Are Logical Operators? Logical operators return a boolean value ( true or false ) and are used to evaluate expressions, especially within conditions like if statements. Operator Name Example Description && Logical AND a && b Returns true if both conditions are true || Logical OR a || b Returns true if either condition is true ! Logical NOT !a Reverses the boolean value ✅ 1. Logical AND ( && ) const age = 25; const isCitizen = true; if (age > 18 && isCitizen) { console.log("You can vote!"); } Short-circuiting: If the first condition is false, the second one isn’t evaluated. ✅ 2. Logi...

⚡ Throttling in JavaScript Explained with Examples

Throttling in JavaScript Thumbnail

Have you ever wondered how to control how often a function runs when triggered frequently — like on scroll, resize, or button click? That's where Throttling in JavaScript comes in.

πŸ“Œ What is Throttling?

Throttling is a technique used to limit the number of times a function gets called over time. Instead of calling the function every time an event fires, it ensures the function executes only once every specified interval.

Use Case: Window resize, scroll tracking, infinite scroll, button spam protection, etc.

🧠 How Throttling Works

Let’s say an event is triggered 50 times per second. With throttling, you can restrict the callback to run only once every 300ms or any interval you decide.

πŸ› ️ Simple Example of Throttling

function throttle(func, limit) {
  let lastFunc;
  let lastRan;
  return function(...args) {
    const context = this;
    if (!lastRan) {
      func.apply(context, args);
      lastRan = Date.now();
    } else {
      clearTimeout(lastFunc);
      lastFunc = setTimeout(function() {
        if ((Date.now() - lastRan) >= limit) {
          func.apply(context, args);
          lastRan = Date.now();
        }
      }, limit - (Date.now() - lastRan));
    }
  }
}

const logScroll = () => console.log("Scroll event at", new Date().toLocaleTimeString());
window.addEventListener('scroll', throttle(logScroll, 1000));

✅ Benefits of Throttling

  • Improves performance by reducing function calls
  • Prevents layout thrashing and jank in UI
  • Helps manage API calls or data fetching efficiently

πŸ€” Throttling vs Debouncing

Both are rate-limiting techniques:

  • Throttling: Ensures function runs at most once in a given interval.
  • Debouncing: Delays the function until after the last event in a series.

πŸ”š Conclusion

Throttling is essential when dealing with high-frequency event handlers. It helps you build efficient and responsive applications by avoiding performance bottlenecks.

πŸ“š Recommended Reading:

  • Debouncing in JavaScript Explained
  • setTimeout vs setInterval: What's the Difference?
  • requestAnimationFrame vs setTimeout
πŸ’‘ Tip: Combine throttling with debouncing for advanced performance control!

Comments

Popular posts from this blog

How to Fix npx tailwindcss init Error: “Could Not Determine Executable to Run”

πŸš€ “JavaScript Debounce Made Simple + Live Example!”

πŸ” Deep Dive into useMemo in React.js: Optimize Performance Like a Pro