DEV Community

ViGnEsH
ViGnEsH

Posted on

"JavaScript Conditional Operator Tutorial (with Example "

Today MARCH 24 2026

Syntax

condition ? valueIfTrue : valueIfFalse;

🧠 What is Conditional Operator?

The conditional (ternary) operator uses ? and : symbols to check a condition.

Basic Example

let age = 18;

let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);
Enter fullscreen mode Exit fullscreen mode

if condition true print = Adult

if false print = Minor


🔄 if-else vs Conditional Operator
if-else

if Conditional is false and else conditional show of console
if condition press can't go else.

if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}
Enter fullscreen mode Exit fullscreen mode

if conditional press

if conditional false ,else condition press


📝** Nested if-else in JavaScript**

🧠 What is Nested if-else?

  • A nested if-else means an if statement inside another if or else.
  • Used when you need to check multiple conditions step by step.

Two value nested if-else


Top comments (0)