✅ 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...

πŸ” What is Closure in JavaScript? ? Explained with Simple Examples

What is closure in JavaScript

In JavaScript, a closure is created when a function "remembers" the variables from its outer scope even after the outer function has finished executing. This concept allows functions to access variables from an enclosing scope or function — even after that outer function has returned.

Closures are a powerful feature in JavaScript that enable data encapsulation, callback handling, and the creation of private variables.


πŸ’‘ Let's Understand with an Example


function outerFunction() {
  let outerVariable = "I am from outer scope!";

  function innerFunction() {
    console.log(outerVariable); // Accessing variable from outer scope
  }

  return innerFunction;
}

const closureFunc = outerFunction();
closureFunc(); // Output: I am from outer scope!

πŸ” Explanation:

  • outerFunction defines a variable outerVariable.
  • innerFunction is declared inside outerFunction and uses that variable.
  • Even after outerFunction() has finished executing, innerFunction() can still access outerVariable.
  • This is closure in action.

πŸ“¦ Why Use Closures?

Closures are used to:

  • Preserve Data between function calls.
  • πŸ” Create private variables in JavaScript.
  • 🧠 Maintain state in asynchronous operations (e.g., setTimeout).
  • 🎯 Implement factory functions that produce custom behavior.

πŸ§ͺ Real-World Use Case: Counter Function


function createCounter() {
  let count = 0;

  return function() {
    count++;
    console.log(count);
  };
}

const counter1 = createCounter();
counter1(); // 1
counter1(); // 2

const counter2 = createCounter();
counter2(); // 1

πŸ” Each counter maintains its own separate copy of count due to closures.


🧠 Key Points to Remember

  • A closure gives you access to an outer function’s variables even after the outer function has returned.
  • Closures help emulate private variables in JavaScript.
  • Closures are created every time a function is created, at function creation time.

🎯 Conclusion

Closures in JavaScript may seem tricky at first, but they are essential for writing modular, maintainable, and efficient code. Understanding closures will help you master more advanced concepts like callbacks, currying, and function factories.

πŸ–±️ Explore this powerful concept in your code today and see the magic of closures in action!


πŸ”— Read more tutorials like this at:

🌐 webcodingwithankur.blogspot.com

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!”

πŸ› ️ How to Create a React App with Vite and Tailwind (Beginner Guide)