JS

JavaScript Fundamentals

25 lessons

Progress0%
1. Introduction to JavaScript
1What is JavaScript?2Setting Up Your Environment
2. Variables and Data Types
1Declaring Variables2Data Types3Type Conversion
3. Operators
Arithmetic OperatorsComparison OperatorsLogical Operators
4. Control Flow
Conditional StatementsLoops
5. Functions
Function Basics
6. Arrays & Iteration
Array MethodsSpread, Rest & Destructuring
7. Objects & JSON
Working with ObjectsJSON & Optional Chaining
8. OOP & Classes
Class BasicsInheritance & Private Fields
9. Modules & Modern JS
ES ModulesModern JavaScript Features
10. Async JavaScript
PromisesAsync/Await
11. Error Handling
Error Types & try/catchCustom Errors & Debugging
12. Iterators & Advanced
Iterators & GeneratorsMap, Set & WeakRefs
All Tutorials
JavaScriptOperators
Lesson 8 of 25 min
Chapter 3 · Lesson 3

Logical Operators

Logical operators work with boolean values and are essential for conditional logic.

Main Logical Operators:

OperatorNameDescription
&&ANDReturns true if BOTH operands are true
!NOTInverts the boolean value

Truth Tables:

code
AND (&&)           OR (||)
T && T = T         T || T = T
T && F = F         T || F = T
F && T = F         F || T = T
F && F = F         F || F = F

Short-Circuit Evaluation:

  • && stops at the first falsy value
  • || stops at the first truthy value

This is useful for:

  • Setting default values: name || "Guest"
  • Conditional execution: isAdmin && deleteUser()

Nullish Coalescing (??): Returns the right side only if the left is null or undefined (not for other falsy values like 0 or "").

Code Examples

Logical Operationsjavascript
// AND (&&) - all must be true
console.log(true && true);    // true
console.log(true && false);   // false
console.log(5 > 3 && 10 > 5); // true

// OR (||) - at least one must be true
console.log(true || false);   // true
console.log(false || false);  // false
console.log(5 > 10 || 3 > 1); // true

// NOT (!) - inverts the value
console.log(!true);           // false
console.log(!false);          // true
console.log(!(5 > 3));        // false

// Short-circuit evaluation
let name = "";
console.log(name || "Guest");      // "Guest" (|| finds truthy)

let user = { name: "Alice" };
console.log(user && user.name);    // "Alice" (&& continues if truthy)

// Nullish coalescing (??)
let count = 0;
console.log(count || 10);    // 10 (0 is falsy)
console.log(count ?? 10);    // 0 (?? only checks null/undefined)

let value = null;
console.log(value ?? "default");   // "default"

Short-circuit evaluation is powerful for setting defaults and conditional execution without if statements.

Quick Quiz

1. What is the result of true && false || true?

2. What does 0 ?? 'default' return?

Was this lesson helpful?

PreviousNext