Hackforge Academy

Category: javascript

Conditional statements

Published on 29 Jun 2026

Explanation

Conditional statements allow JavaScript to execute different code blocks based on conditions.

Code:

let age = 20;
if (age >= 18) {
  console.log('Adult');
}

Explanation

The if...else statement executes one block if the condition is true and another if it is false.

Code:

let marks = 40;
if (marks >= 50) {
  console.log('Pass');
} else {
  console.log('Fail');
}

Explanation

The else if statement checks multiple conditions sequentially.

Code:

let score = 85;
if (score >= 90) {
  console.log('A');
} else if (score >= 75) {
  console.log('B');
}

Explanation

The switch statement is useful when comparing a single value against multiple cases.

Code:

let day = 2;
switch(day) {
  case 1: console.log('Monday'); break;
  case 2: console.log('Tuesday'); break;
}

Explanation

The ternary operator provides a concise way to write simple if...else conditions.

Code:

let age = 18;
let result = age >= 18 ? 'Eligible' : 'Not Eligible';

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community