# Conditionals and Logical Operators

**Conditionals**

The steps your code takes to solve a problem is known as an algorithm.
A flowchart is just a diagram for how to solve a problem.
DEFINITION: A flowchart is a visual diagram that outlines the solution to a problem through a series of logical statements. The order in which statements are evaluated and executed is called the control flow.

**If...else statements**

If...else statements allow you to execute certain pieces of code based on a condition, or set of conditions.

```
if (/* this expression is true */) {
  // run this code
} else {
  // run this code
}
``` 

```
var a = 3;
var b = 5;

if (a > b) {
  console.log("a is greater than b");
} else {
  console.log("a is less than or equal to b");
}
Prints: "a is less than or equal to b"
``` 
**Else if statements**

In JavaScript, you can represent this secondary check by using an extra if statement called an else if statement

```
var weather = "sunny";

if (weather === "snow") {
  console.log("Bring a coat.");
} else if (weather === "rain") {
  console.log("Bring a rain jacket.");
} else {
  console.log("Wear what you have on.");
}
Prints: Wear what you have on.
``` 
The else statement essentially acts as the "default" condition in case all the other if statements are false.

**More Complex Problems**

In javascript we can represent more complex problems by combining logical expressions with special operators called logical operators

```
var frnd = "not busy";
var weather = "nice";

if (frnd === "not busy" && weather === "nice") {
  console.log("go to the park");
}
Prints: "go to the park"
``` 
The && symbol is the logical AND operator, and it is used to combine two logical expressions into one larger logical expression. If both smaller expressions are true, then the entire expression evaluates to true. If either one of the smaller expressions is false, then the whole logical expression is false.

**Logical expressions**

Logical expressions are similar to mathematical expressions, except logical expressions evaluate to either true or false.

```
11 != 12
Returns: true
``` 
Similar to mathematical expressions that use +, -, *, / and %, there are logical operators &&, || and ! that you can use to create more complex logical expressions.

**Logical operators**

- Logical operators can be used in conjunction with boolean values (true and false) to create complex logical expressions.

- By combining two boolean values together with a logical operator, you create a logical expression that returns another boolean value. Here’s a table describing the different logical operators:

![StaticShot_02-08-2022_21-59-07.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659457775444/dIwrNk4OH.png align="left")

**Truth tables**

Truth tables are used to represent the result of all the possible combinations of inputs in a logical expression

![StaticShot_02-08-2022_22-07-46.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659458307867/dzB6F4htr.png align="left")
In both tables, there are specific scenarios where regardless of the value of B, the value of A is enough to satisfy the condition.

For example, if you look at A AND B, if A is false, then regardless of the value B, the total expression will always evaluate to false because both A and B must be true in order for the entire expression to be true.

This behavior is called *short-circuiting* because it describes the event when later arguments in a logical expression are not considered because the first argument already satisfies the condition.

**Truthy and Falsy**

*Falsy values*

A value is falsy if it converts to false when evaluated in a boolean context. For example, an empty String "" is falsy because, "" evaluates to false. You already know if...else statements, so let's use them to test the truthy-ness of "".

```
 if ("") {
    console.log("the value is truthy");
} else {
    console.log("the value is falsy");
}
Returns: "the value is falsy"
``` 
Here’s the list of all of the falsy values:
the Boolean value false
the null type
the undefined type
the number 0
the empty string ""
the odd value NaN (stands for "not a number")
There are only six falsy values in all of JavaScript!

*Truthy values*

A value is truthy if it converts to true when evaluated in a boolean context. For example, the number 1 is truthy because, 1 evaluates to true. Let's use an if...else statement again to test this out:

```
if (1) {
    console.log("the value is truthy");
} else {
    console.log("the value is falsy");
}
Returns: "the value is truthy"
```

**Ternary operator**

The ternary operator provides you with a shortcut alternative for writing lengthy if...else statements.

```
conditional ? (if condition is true) : (if condition is false)
```
To use the ternary operator, first provide a conditional statement on the left-side of the ?. Then, between the ? and : write the code that would run if the condition is true and on the right-hand side of the : write the code that would run if the condition is false. 
```
var isGoing = true;
var color = isGoing ? "blue" : "purple";
console.log(color);
Prints: "blue"
```

This code not only replaces the conditional, but it also handles the variable assignment for color.

If you find yourself repeating else if statements in your code, where each condition is based on the same value, then it might be time to use a switch statement.
```
if (option === 1) {
  console.log("You selected option 1.");
} else if (option === 2) {
  console.log("You selected option 2.");
} else if (option === 3) {
  console.log("You selected option 3.");
} else if (option === 4) {
  console.log("You selected option 4.");
} else if (option === 5) {
  console.log("You selected option 5.");
} else if (option === 6) {
  console.log("You selected option 6.");
}
``` 
**Switch statement**

A switch statement is an another way to chain multiple else if statements that are based on the same value without using conditional statements

```
var option = 3;
switch (option) {
  case 1:
    console.log("You selected option 1.");
  case 2:
    console.log("You selected option 2.");
  case 3:
    console.log("You selected option 3.");
  case 4:
    console.log("You selected option 4.");
  case 5:
    console.log("You selected option 5.");
  case 6:
    console.log("You selected option 6.");
}
Prints:
You selected option 3.
You selected option 4.
You selected option 5.
You selected option 6.
``` 
**Break statement**

The break statement can be used to terminate a switch statement and transfer control to the code following the terminated statement. By adding a break to each case clause, you fix the issue of the switch statement falling-through to other case clauses.

```
var option = 3;

switch (option) {
  case 1:
    console.log("You selected option 1.");
    break;
  case 2:
    console.log("You selected option 2.");
    break;
  case 3:
    console.log("You selected option 3.");
    break;
  case 4:
    console.log("You selected option 4.");
    break;
  case 5:
    console.log("You selected option 5.");
    break;
  case 6:
    console.log("You selected option 6.");
    break; // technically, not needed
}
Prints: You selected option 3.
``` 






