# If Else and Else If Statements

**URL:** <https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142>\
**Category:** General\
**Created:** [May 17, 2020, 10:46am UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142 "2020-05-17T10:46:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![allyza.fabellar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/allyza.fabellar/32/148157_2.png) [@allyza.fabellar](https://forum.freecodecamp.org/u/allyza.fabellar)\
**Post date:** [May 17, 2020, 10:46am UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142/1 "2020-05-17T10:46:26Z")

</div>

Hi! Newbie here. I just want to ask: What is the major difference between if else and else if statement? What is the proper way of using them? Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [May 17, 2020, 10:56am UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142/2 "2020-05-17T10:56:12Z")

</div>

If/Else indicate an if statement followed by an else statement:

```auto
if (condition) {
   ...
} else {
   ...
}

```

`else if` statements are a specific type of statement, which can be put only after an if statement, so like:

```auto
if (condition1) {
   ...
} else if (condition2) {
   ...
} 

```

you can put as many `else if` statements as needed.

You can create an if/else if/else chain starting with an `if` statement, followed by as many `else if` statements as needed and then one `else` statement to close

---

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [May 17, 2020, 11:09am UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142/3 "2020-05-17T11:09:12Z")

</div>

Let me illustrate with an example.

Use of `if else`

* * *

```auto
// Let us guess allyza.fabellar's age

let age = 30;

if( age < 18){
     console.log( "She is a child")
} else{
     console.log( "She is an adult")
}

```

In the code above `She is an adult` will be console.logged  
Use of `if, else if and else`

* * *

```auto
// Let us guess allyza.fabellar's age

let age = 80;

if( age < 18){
     console.log( "She is a child")
}
else if (age >= 18 && age < 35){
     console.log( "She is a youth")
} 
else if (age >= 35 && age < 50){
     console.log("She is middle aged") // I don't know what middle aged means
}else{

     console.log("She is elderly")
}

```

In the code above `She is elderly` will be console.logged  
NOTE:

1. `else if` statements are optional. What you need is `if` and `else` like in the first example
2. `else if` statements always go between `if` and `else` statements
3. You can have as many `else if` statements as possible
4. Don’t use `if else` in place of `else if`

---

<div class="post-metadata">

**Author:** ![allyza.fabellar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/allyza.fabellar/32/148157_2.png) [@allyza.fabellar](https://forum.freecodecamp.org/u/allyza.fabellar)\
**Post date:** [May 17, 2020, 11:34am UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142/4 "2020-05-17T11:34:17Z")

</div>

Got it! Thank you so much @ILM @nibble for sharing your knowledge. I understand it clearly now 😁

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:53pm UTC](https://forum.freecodecamp.org/t/if-else-and-else-if-statements/390142/5 "2021-06-01T18:53:35Z")

</div>

Closed on June 1, 2021, 6:53pm UTC


