JavaScript Logical Operators

JavaScript Logical Operators

JavaScript Logical Operators

Logical operators in JavaScript are used to combine or negate Boolean values and expressions. They return true or false.

1️⃣ Logical Operators in JavaScript

OperatorNameExampleDescription
&&Logical ANDa && bReturns true if both a and b are true.
``Logical OR
!Logical NOT!aReturns true if a is false, and vice versa.

2️⃣ Logical AND (&&)

✅ Returns true only if both conditions are true.
🚫 If any condition is false, it returns false.

console.log(true && true); // true console.log(true && false); // false console.log(5 > 3 && 10 < 20); // true console.log(5 > 3 && 10 > 20); // false

🔹 Use Case: Checking multiple conditions

let age = 25; let hasID = true; if (age >= 18 && hasID) { console.log("You can enter."); } else { console.log("Access denied."); }

3️⃣ Logical OR (||)

✅ Returns true if at least one condition is true.
🚫 Returns false only if both conditions are false.

console.log(true || false); // true console.log(false || false); // false console.log(5 > 10 || 20 > 15); // true console.log(5 > 10 || 20 < 15); // false

🔹 Use Case: Providing default values

let username = ""; let displayName = username || "Guest"; // If username is empty, use "Guest" console.log(displayName); // "Guest"

4️⃣ Logical NOT (!)

Negates a Boolean value (truefalse, falsetrue).

console.log(!true); // false console.log(!false); // true console.log(!(5 > 3)); // false (since 5 > 3 is true)

🔹 Use Case: Checking if a variable is falsy

let isLoggedIn = false; if (!isLoggedIn) { console.log("Please log in."); }

5️⃣ Short-Circuiting in Logical Operators

Logical AND (&&) Short-Circuiting

If the first operand is false, JavaScript stops evaluating and returns that value.

console.log(false && "Hello"); // false (does not evaluate "Hello") console.log(true && "Hello"); // "Hello" (evaluates second operand)

Logical OR (||) Short-Circuiting

If the first operand is true, JavaScript stops evaluating and returns that value.

console.log(true || "Hello"); // true (stops at first operand) console.log(false || "Hello"); // "Hello" (evaluates second operand)

🔹 Use Case: Setting default values

let user = null; let userName = user || "Anonymous"; // If user is null, use "Anonymous" console.log(userName); // "Anonymous"

6️⃣ Combining Logical Operators

You can combine &&, ||, and ! in complex conditions.

let age = 20; let hasLicense = true; if (age >= 18 && hasLicense) { console.log("You can drive."); } else { console.log("You cannot drive."); }

🎯 Summary

&& (AND) → Both must be true
|| (OR) → At least one must be true
! (NOT) → Reverses the Boolean value
Short-circuiting improves performance
Use OR (||) for default values

🚀 Now you can handle logical conditions in JavaScript like a pro! Let me know if you need more details. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close