#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Logical

Logical operators combine or invert conditions. JavaScript has three classic ones, && (AND), || (OR) and ! (NOT), plus the nullish coalescing operator ??. You use them to check several requirements at once, to provide fallback values, and to run code only when something exists. They behave a little differently from logical operators in some other languages, because they return one of their operands rather than always returning a boolean.

AND, OR and NOT

  • a && b is truthy only when both sides are truthy.
  • a || b is truthy when at least one side is truthy.
  • !a flips truthiness: !true is false, !"" is true. Two of them, !!value, convert any value to a real boolean.
const age = 22;
const hasId = true;

if (age >= 18 && hasId) {
  console.log('Entry allowed');
}

const isWeekend = day => day === 'Sat' || day === 'Sun';

// They return an operand, not just true/false
console.log('Asha' && 'Ravi');  // "Ravi"
console.log('' || 'Default');    // "Default"
console.log(0 || 50);            // 50
console.log(0 ?? 50);            // 0
console.log(null ?? 50);         // 50

// Logical assignment
const settings = { theme: '' };
settings.theme ||= 'light';   // '' is falsy, so becomes 'light'
settings.volume ??= 70;       // only set if null/undefined

Short-circuit evaluation

JavaScript stops evaluating as soon as the result is known. With &&, if the left side is falsy, the right side never runs and the left value is returned. With ||, if the left side is truthy, the right side is skipped and the left value is returned. This is why user && user.logout() safely calls the method only when user exists. Today, optional chaining, user?.logout(), is often a clearer way to write that.

|| versus ?? for defaults

A long-standing habit is writing const qty = input || 1 to supply a default. The problem is that || replaces every falsy value, including 0, an empty string and false, which may all be valid input. If a user chooses quantity 0, it silently becomes 1. The ?? operator only falls back when the left side is null or undefined, so input ?? 1 keeps a real zero. Prefer ?? for defaults unless you truly want to treat every falsy value as missing.

Operator precedence

! binds tightest, then &&, then ||. So a || b && c means a || (b && c). Add parentheses whenever you mix them; it costs nothing and removes doubt. Mixing ?? directly with && or || without parentheses is a syntax error by design.

Optional chaining pairs naturally with ??. An expression such as user?.address?.city ?? 'Not provided' reads a nested value safely and supplies a label when any part of the path is missing.

A negation slip

Writing !a == b negates a first and then compares, which is rarely what was intended. Use a !== b. Similarly, reversing a combined condition needs care: the opposite of age >= 18 && hasId is age < 18 || !hasId, not age < 18 && !hasId. Swapping AND for OR when negating is known as De Morgan's law.

Main points

Use && and || to combine conditions, rely on short-circuiting for guarded calls, choose ?? when zero or empty strings are valid, and add parentheses when operators mix.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course