$100 Website Offer

Get your personal website + domain for just $100.

Limited Time Offer!

Claim Your Website Now

JavaScript Condition

In JavaScript, you can use conditional statements to execute different code blocks based on different conditions. The main types of conditional statements are:

1. If Statement:

The if statement executes a block of code if a specified condition is true.

let x = 10;

if (x > 5) {
    console.log("x is greater than 5");
}

2. If-Else Statement:

The if-else statement executes one block of code if the condition is true, and another block if the condition is false.

let x = 3;

if (x % 2 === 0) {
    console.log("x is even");
} else {
    console.log("x is odd");
}

3. Else-If Statement:

The else if statement allows you to specify multiple conditions to be tested.

let x = 0;

if (x > 0) {
    console.log("x is positive");
} else if (x < 0) {
    console.log("x is negative");
} else {
    console.log("x is zero");
}

4. Switch Statement:

The switch statement allows you to test a variable against multiple values and execute different code blocks based on which value it equals to.

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("It's Monday!");
        break;
    case "Tuesday":
        console.log("It's Tuesday!");
        break;
    default:
        console.log("It's another day of the week");
}

5. Ternary Operator:

The ternary operator ? : is a concise way to write conditional statements.

let x = 10;
let message = (x > 5) ? "x is greater than 5" : "x is less than or equal to 5";
console.log(message);

Conditional statements are fundamental for controlling the flow of your JavaScript code, allowing you to execute different blocks of code based on specific conditions. They are powerful tools for building dynamic and interactive applications.

6. Switch Statements:

Switch statements in JavaScript provide a way to execute different code blocks based on the value of an expression. The syntax of a switch statement looks like this:

switch (expression) {
  case value1:
    // Code block to be executed if expression matches value1
    break;
  case value2:
    // Code block to be executed if expression matches value2
    break;
  // Additional cases as needed
  default:
    // Code block to be executed if expression doesn't match any case
}

Here’s how it works:

  • The switch keyword is followed by the expression whose value you want to test.
  • Each case specifies a value to compare the expression against. If the expression matches a case value, the corresponding code block is executed.
  • The break statement is used to exit the switch block once a match is found. If omitted, execution will continue to the next case, even if the condition doesn’t match.
  • The default case is optional and is executed if none of the cases match the expression.

Example:

let day = 3;
let dayName;

switch (day) {
  case 1:
    dayName = 'Monday';
    break;
  case 2:
    dayName = 'Tuesday';
    break;
  case 3:
    dayName = 'Wednesday';
    break;
  case 4:
    dayName = 'Thursday';
    break;
  case 5:
    dayName = 'Friday';
    break;
  case 6:
    dayName = 'Saturday';
    break;
  case 7:
    dayName = 'Sunday';
    break;
  default:
    dayName = 'Invalid day';
}

console.log(dayName); // Output: Wednesday

Switch statements are useful when you have a single expression with multiple possible values and want to execute different code based on each value. They provide a cleaner and more readable alternative to multiple if-else statements in such cases.

Related Posts

Moodle: Cohorts, Cohort Sync, and Course Meta Link — Complete Guide for Course Access Management

1. What Is a Moodle Cohort? A cohort in Moodle is a collection of users created at the site or category level. You can think of it…

Read More

Moodle: How to Fix Moodle “Can’t Find Data Record in Database” Error in Enrolment Methods After Deleting Linked Courses

Managing enrolments in Moodle is usually simple when everything is cleanly configured. But sometimes, after deleting old courses or changing the enrolment structure, Moodle may suddenly start…

Read More

Establishing Operational Standards via the Certified DataOps Manager Certification

Introduction Data operations have shifted from a niche engineering requirement to a core business necessity. The CDOM – Certified DataOps Manager program is designed for professionals looking…

Read More

Dominate Modern Data Pipelines: The Complete Certified DataOps Architect Roadmap

Introduction Modern enterprise information systems are encountering massive scaling bottlenecks, demanding systematic approaches to handle data pipeline dependability, throughput, and agility. The CDOA – Certified DataOps Architect…

Read More

The Definitive Roadmap to CDOE – Certified DataOps Engineer Success

Introduction The modern data landscape is shifting rapidly, requiring engineering practices that match the speed and reliability of software development. This guide introduces the CDOE – Certified…

Read More

Maximizing Your DevOps Compensation: Earning Trends, Career Trajectories, and Premium Tech Skills

The technology sector suffers from massive title ambiguity. The label “DevOps salary” is attached to everything from junior script maintenance to the architectural oversight of globally distributed,…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x