Conditional Statements

Conditional Statements in JavaScript are used to make decisions in programs based on conditions.
They help the program choose different actions depending on whether a condition is true or false.
Conditional statements improve the logic and intelligence of applications and websites.
JavaScript mainly uses if, else if, else, and switch statements for decision-making.
These statements are very important in web development, games, login systems, and calculators.
Learning conditional statements helps students develop strong problem-solving and programming skills.

Conditional statements run different code depending on true or false conditions.

Conditional statements include:

  • if
  • if...else
  • if...else if...else
  • switch
  • ternary (? :)

What are Conditional Statements?

A Conditional Statement is a programming structure that checks a condition and executes a specific block of code based on the result.

Example

let age = 18;
 if(age >= 18){ 
console.log("Eligible to Vote"); 
}

In this example:

  • The condition is age >= 18
  • If the condition is true, the message is displayed
  • If the condition is false, the code is skipped

Why are Conditional Statements Important

Conditional statements help programs:

  • Make decisions automatically
  • Validate user input
  • Control program flow
  • Improve user experience
  • Build dynamic applications

When to use Conditionals

  • Use if to specify a code block to be executed, if a specified condition is true
  • Use else to specify a code block to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative code blocks to be executed
  • Use (? :) (ternary) as a shorthand for if...else

Types of Conditional Statements in JavaScript

Conditional Statement Purpose
if Executes code when a condition is true
if...else Executes one block if true and another if false
else if Checks multiple conditions
switch Selects one option from multiple choices
Ternary Operator (? :) Short form of if...else

Explanation

  • Conditional statements help programs make smart decisions.
  • The if statement executes code only when a condition is true.
  • The else statement runs when the condition becomes false.
  • else if is used to check multiple conditions.
  • Conditional statements improve website interactivity and user experience.
  • They are widely used in login systems, games, forms, and result applications.
  • JavaScript conditions mainly return true or false values.

Real-Time Scenario

  • Login systems check whether the username and password are correct.
  • ATM machines verify whether the PIN number is valid.
  • Online shopping websites apply discounts based on purchase amount.
  • School result systems decide whether a student passes or fails.
  • Games unlock new levels based on the player’s score.
  • Traffic signal systems use conditions to control red, yellow, and green lights.
  • Social media apps show notifications based on user activity.

Syntax

if(condition) {
    // code
}

if(condition) {
    // true block
}
else {
    // false block
}

if(condition1) {
    // code
}
else if(condition2) {
    // code
}
else {
    // code
}

Example 

<!DOCTYPE html>
<html>

<head>
    <title>Conditional Statements</title>
</head>

<body>

<h1>JavaScript Conditional Statements</h1>

<p id="demo"></p>

<script>

let marks = 75;

if(marks >= 90) {
    document.getElementById("demo").innerHTML = "Grade A";
}
else if(marks >= 50) {
    document.getElementById("demo").innerHTML = "Grade B";
}
else {
    document.getElementById("demo").innerHTML = "Fail";
}

</script>

</body>
</html>

Output

Grade B

Difference Between if, else if, switch, and Ternary Operator

Feature if else if switch Ternary
Checks Single Condition Yes No No Yes
Checks Multiple Conditions No Yes Limited No
Easy to Read Yes Medium High High
Best For Simple Conditions Multiple Conditions Multiple Fixed Values Short Conditions
Code Length Medium Long Medium Short

Summary

Conditional Statements in JavaScript are essential decision-making tools that allow programs to execute different actions based on conditions. JavaScript provides several conditional structures including if, if...else, if...else if...else, switch, and the Ternary Operator. These statements are widely used in login systems, online shopping websites, games, banking applications, and educational platforms. Mastering conditional statements helps students build logical thinking, problem-solving skills, and a strong foundation in JavaScript programming and web development.

Keywords

Conditional Statements in JavaScript, JavaScript if Statement, JavaScript if else, JavaScript else if, JavaScript switch Statement, JavaScript Ternary Operator, Decision Making in JavaScript, JavaScript Programming Tutorial, JavaScript Examples, JavaScript Web Development, Learn JavaScript for Beginners, JavaScript Interview Questions, Frontend Development, JavaScript Coding Concepts

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What is the main purpose of conditional statements in JavaScript?

Conditional statements help JavaScript programs make logical decisions.

Question 2

Which statement is used to check a condition in JavaScript?

The if statement checks whether a condition is true or false.

Question 3

Which statement runs when the condition is false?

The else statement executes when the condition becomes false.

Question 4

Which conditional statement is used for multiple conditions?

The else if statement helps check multiple conditions in JavaScript.

Question 5

Which value is mainly used in conditional statements?

Conditional statements work mainly with Boolean values like true and false.

Congratulations!

You've successfully mastered the knowledge check for "Conditional Statements."

Keep up the great work and continue your learning journey!

Previous Topic Operators Next Topic Loops