✅ 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 Hoisting in JavaScript? πŸ”„ Explained with Example

πŸ“Œ What is Hoisting in JavaScript?

In this post, we’ll understand one of the most commonly asked JavaScript interview questions: What is Hoisting? I’ve also added a video below that explains hoisting visually. πŸ‘‡

Video: What is Hoisting in JavaScript

πŸ” What is Hoisting?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

🧠 Think of it like this:

Even if you declare your variables or functions at the bottom of the file, JavaScript will act as if they were declared at the top — but only the declarations, not initializations.

πŸ“‚ Example 1: Variable Hoisting

console.log(x); // undefined
var x = 10;

πŸ”Ž Explanation: The declaration var x is hoisted to the top, but the assignment = 10 is not. So x exists but is undefined at the time of the console.log.

⚠️ Let’s Try with let or const

console.log(y); // ReferenceError
let y = 20;

Note: Variables declared with let and const are also hoisted but are not initialized. They remain in a temporal dead zone until the declaration is encountered.

πŸ“˜ Function Hoisting

greet(); // "Hello!"
function greet() {
  console.log("Hello!");
}

✅ Function declarations are fully hoisted. So you can call the function before it's defined.

πŸ“Ί Watch the Video

For a complete breakdown with visuals and explanations, watch the full video above or on YouTube.

πŸ”— Useful Links

Thanks for reading! If you found this helpful, don’t forget to leave a comment and share this post with fellow developers. πŸš€

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