# Conditional statements

Conditional statements are programming constructs that allow programmers to make decisions and execute different blocks of code based on a certain condition. There are three main types of conditional statements: if-else statements, switch statements, and ternary operators.

1. If-else statements: An if-else statement is used to execute a block of code if a certain condition is true, and a different block of code if the condition is false. The general syntax of an if-else statement is as follows:

```javascript
if (condition) {
    // block of code to execute if the condition is true
} else {
    // block of code to execute if the condition is false
}
```

2. Switch statements: A switch statement is used to execute different blocks of code based on the value of a variable or expression. The general syntax of a switch statement is as follows:

```javascript
switch (expression) {
    case value1:
        // block of code to execute if expression is equal to value1
        break;
    case value2:
        // block of code to execute if expression is equal to value2
        break;
    default:
        // block of code to execute if none of the above cases are true
        break;
}
```

3. Ternary operators: A ternary operator is a shorthand way of writing an if-else statement that evaluates a single expression and returns one of two possible values depending on whether the condition is true or false. The general syntax of a ternary operator is as follows:

```
condition ? value1 : value2
```

If the condition is true, the operator returns value1; otherwise, it returns value2.

Conditional statements are essential in programming because they allow programs to make decisions and take different actions based on different inputs or conditions.

Here are some examples of conditional statements in JavaScript:

1. If statement:

The if statement is used to execute a block of code only if a specific condition is true.

Example:

```javascript
let age = 18;
if(age >= 18) {
  console.log("You are eligible to vote");
}
```

In this example, if the `age` variable is greater than or equal to 18, the message "You are eligible to vote" will be displayed in the console.

2. If-else statement:

The if-else statement is used to execute a block of code if a specific condition is true and another block of code if the condition is false.

Example:

```javascript
let num = 5;
if(num % 2 === 0) {
  console.log(num + " is an even number");
} else {
  console.log(num + " is an odd number");
}
```

In this example, if the `num` variable is divisible by 2, the message "5 is an odd number" will be displayed in the console.

3. If-else-if statement:

The if-else-if statement is used to execute different blocks of code for different conditions.

Example:

```javascript
let grade = 80;
if(grade >= 90) {
  console.log("You got an A");
} else if(grade >= 80) {
  console.log("You got a B");
} else if(grade >= 70) {
  console.log("You got a C");
} else {
  console.log("You failed");
}
```

In this example, if the `grade` variable is greater than or equal to 90, the message "You got an A" will be displayed in the console. If the `grade` is between 80 and 89, the message "You got a B" will be displayed, and so on.

Switch Statement Example:

```javascript
let day = "Monday";

switch(day) {
  case "Monday":
    console.log("Today is Monday");
    break;
  case "Tuesday":
    console.log("Today is Tuesday");
    break;
  case "Wednesday":
    console.log("Today is Wednesday");
    break;
  default:
    console.log("Today is some other day");
}
```

In this example, we have a switch statement that checks the value of the variable `day`. Depending on the value of `day`, it will execute the corresponding code block. If `day` is "Monday", it will print "Today is Monday" to the console. If `day` is "Tuesday", it will print "Today is Tuesday", and so on. If `day` doesn't match any of the cases, it will execute the `default` case and print "Today is some other day".

Ternary Operator Example:

```javascript
let age = 20;

let message = (age >= 18) ? "You are an adult" : "You are not an adult";

console.log(message);
```

In this example, we have a ternary operator that checks if the variable `age` is greater than or equal to 18. If it is, it will set the value of the variable `message` to "You are an adult". If it's not, it will set the value of `message` to "You are not an adult". The ternary operator is a shorthand way of writing an `if-else` statement.

These are just a few examples of conditional statements in JavaScript. There are many other types of conditional statements, which can be used to create more complex logic in your code.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dhrubo.gitbook.io/programming-with-js/basic-syntax/conditional-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
