JavaScript && || !

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

&& and || short-circuit: they stop when the result is known. ! negates truthiness. They return an operand value, not always a boolean.

JavaScript Syntax

JavaScript
console.log(true && "ok");
console.log(false || "fallback");
console.log(!"");
Try it Editor

How It Works

  1. &&. Returns the first falsy or the last value.
  2. ||. Returns the first truthy.
  3. !. Boolean negation.

Examples

Example 1 — Guard with &&

Run code only if left is truthy.

JavaScript
const user = { name: "Ada" };
user && console.log(user.name);
Try it Editor

How It Works

  1. user is truthy, so right side runs.

Example 2 — Default with ||

Careful: 0 becomes fallback.

JavaScript
console.log(0 || 5);
Try it Editor

How It Works

  1. Prefer ?? when 0 is valid.

Example 3 — Double not !!

Force a boolean.

JavaScript
console.log(!!"hi");
Try it Editor

How It Works

  1. !! converts to boolean.

Frequently Asked Questions

No. They return one of the operands.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful