MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Logical Operators0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
3.1Arithmetic Operators3.2Comparison Operators3.3Logical Operators3.4Assignment & Ternary Operator
HomeTutorialsJavaScript for BeginnersLogical Operators
Assignment & Ternary Operator

Logical Operators

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Logical operators are used to combine multiple boolean conditions and make complex decisions in programs. In JavaScript, logical operators have a powerful feature called short-circuit evaluation that allows them to return actual values rather than just booleans.


1. The Three Core Logical Operators

OperatorNameSyntaxDescription
&&Logical ANDA && BReturns true ONLY if both operands are truthy
**``**Logical OR`AB`Returns true if at least one operand is truthy
!Logical NOT!AInverts the boolean truthiness of operand

2. Truth Tables

Logical AND (&&):

1console.log(true && true); // true
2console.log(true && false); // false
3console.log(false && true); // false
4console.log(false && false); // false

Logical OR (||):

1console.log(true || true); // true
2console.log(true || false); // true
3console.log(false || true); // true
4console.log(false || false); // false

Logical NOT (!):

1console.log(!true); // false
2console.log(!false); // true
3 
4// Double NOT (!!) - Converts ANY value to its strict boolean equivalent:
5console.log(!!"Hello"); // true
6console.log(!!0); // false
7console.log(!!null); // false

3. Short-Circuit Evaluation: How JS Actually Evaluates

In JavaScript, && and || do not simply return true or false. They return the value of the actual operand that decided the outcome!

How Logical AND (&&) Short-Circuits:

  • Evaluates from left to right.
  • If it encounters a falsy value, it stops immediately and returns that falsy value!
  • If all values are truthy, it returns the last value.
1console.log("Apple" && "Banana"); // "Banana" (Both truthy, returns last)
2console.log(null && "Banana"); // null (Short-circuits on null!)
3console.log(0 && "Orange"); // 0 (Short-circuits on 0!)

Practical React & UI Guard Pattern:

1// Only render user avatar if user is logged in
2isLoggedIn && renderAvatar();

How Logical OR (||) Short-Circuits:

  • Evaluates from left to right.
  • If it encounters a truthy value, it stops immediately and returns that truthy value!
  • If all values are falsy, it returns the last value.
1console.log("Default" || "Fallback"); // "Default" (First truthy)
2console.log("" || "Anonymous"); // "Anonymous" (Empty string is falsy)
3console.log(null || undefined || "Guest"); // "Guest"

Practical Default Fallback Pattern:

1const displayName = inputName || "Guest User";

4. The Modern Nullish Coalescing Operator (??) (ES2020)

Notice a flaw with ||: what if a valid value is 0 or empty string ""?

1let score = 0; // Valid game score!
2let finalScore = score || 10;
3console.log(finalScore); // 10! (Bug! 0 was treated as falsy!)

The Fix: Nullish Coalescing (??)

?? checks strictly for null or undefined, treating 0 and "" as valid truthy values!

1let score = 0;
2let finalScore = score ?? 10;
3console.log(finalScore); // 0! (Preserves 0 perfectly!)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does the expression true && false evaluate to?
Question 2
What is the output of "Hello" && "World" in JavaScript?
Question 3
What is the output of "" || "Default Name"?
Question 4
How does the Nullish Coalescing operator (??) differ from the Logical OR operator (||)?
Question 5
What does the double exclamation mark (!!) do when prefixed before a variable (e.g. !!user)?
Next Lesson

Assignment & Ternary Operator

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Comparison OperatorsAssignment & Ternary Operator

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext