# If else chains Basic JS

**URL:** <https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [October 14, 2019, 1:01pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676 "2019-10-14T13:01:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheMightyRa](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@TheMightyRa](https://forum.freecodecamp.org/u/TheMightyRa)\
**Post date:** [October 14, 2019, 1:01pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/1 "2019-10-14T13:01:52Z")

</div>

I am confused about how to go about doing this. Basic JavaScript: Chaining If else statements

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [October 14, 2019, 1:47pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/3 "2019-10-14T13:47:18Z")

</div>

[https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/)

- Use `if` to specify a block of code to be executed, if a specified condition is true
- Use `else` to specify a block of code 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 blocks of code to be executed

[https://www.w3schools.com/js/js\_if\_else.asp](https://www.w3schools.com/js/js_if_else.asp)

---

<div class="post-metadata">

**Author:** ![TheMightyRa](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@TheMightyRa](https://forum.freecodecamp.org/u/TheMightyRa)\
**Post date:** [October 14, 2019, 3:30pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/4 "2019-10-14T15:30:07Z")

</div>

The type of confusion I am having involves my belief which is this:  
In order for you to completely understand something (like languages) you have to be able to read it and get it then be able to spit it right back out understanding what you are saying.

I am having trouble spitting it right back out.

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 3:34pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/5 "2019-10-14T15:34:22Z")

</div>

I would say try and explain it here to us right now, that way you can rework your understanding of it with some help from the forum.

Like tell me in your own words how you chain if/else statements and why you would do that in the first place.

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [October 14, 2019, 3:38pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/6 "2019-10-14T15:38:00Z")

</div>

if i don’t go to movies at 16:00 today  
else if i will go to the city at 20:00  
else i don’t do them either I will stay at home

```auto
if (time < 10) {
  i go to = "movie";
} else if (time < 20) {
  i go to = "city";
} else {
  i go to = "My home";
}

```

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [October 14, 2019, 3:42pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/7 "2019-10-14T15:42:07Z")

</div>

yes good idea :3 ra why don’t you try it

---

<div class="post-metadata">

**Author:** ![TheMightyRa](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@TheMightyRa](https://forum.freecodecamp.org/u/TheMightyRa)\
**Post date:** [October 14, 2019, 3:50pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/8 "2019-10-14T15:50:14Z")

</div>

I just realized that I do not know how to chain the if/else statements together. I think I understand the English translation of the JavaScript that KittyKora put here:

If you weren’t going to the movies at that time  
You would go to the city at another time  
If you do neither of them, you will stay at home

Is the chain the code?

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 3:53pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/9 "2019-10-14T15:53:46Z")

</div>

I get what you’re trying to show, but “go to” isn’t anything. You should give an example with proper syntax like:

```auto
function checkAge(age){

    if (age < 18) {
    return "Sorry, you can't enter";
    }

    else if (age >= 18 && age < 21){
    return "You can enter, but you can't drink.";
    }

    else {
    return "You can enter and drink";
    }

}

console.log(checkAge(19));

// with the 19 passed into the function, you'd get "You can enter, but you can't drink"

```

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 3:54pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/10 "2019-10-14T15:54:15Z")

</div>

check my example above 😃

Basically we’re building a function that checks the number provided for age (in this case 19).

IF the age provided is less than 18, this person gets bounced, they can’t come in.

Else IF the person is over 18 but younger than 21, they can come in, but they can’t drink alcohol (hahah going by the US age limits)

Else neither of the above statements pass as true, so therefor this person must be at least 21 or over, so they can come in and drink

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 4:11pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/11 "2019-10-14T16:11:50Z")

</div>

Here’s an example with strings:

```auto
function gateKeeper(password){

const correctPass = 'apples';

    if (password === correctPass) {
    return "You may enter!";
    }

    else if (password === ''){
    return "Say the password.";
    }

    else {
    return "That is the wrong password!";
    }

}

console.log(gateKeeper('oranges')); // That is the wrong password!
console.log(gateKeeper('')); // Say the password
console.log(gateKeeper('apples')); // You may enter!

```

---

<div class="post-metadata">

**Author:** ![TheMightyRa](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@TheMightyRa](https://forum.freecodecamp.org/u/TheMightyRa)\
**Post date:** [October 14, 2019, 4:21pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/12 "2019-10-14T16:21:58Z")

</div>

I think I understand: Would this be it? (answering the lesson as well)

```auto
if ( num < 4) {
return "Tiny";
}
else if ( num < 8) {
return "Small";
}
else if (num < 14 ) {
return "Medium";
}
else {
return "Large";
}

```

For “complex logic”, is this correct?

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 4:27pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/13 "2019-10-14T16:27:55Z")

</div>

Perfect! (Granted I’d have to see what the challenge is calling for) but just running this code with different numbers returns the strings you set up.

Keep in mind too, if you end up needing a ton of conditional statements (like more than you have here), you might want to consider using a Switch statement instead.

---

<div class="post-metadata">

**Author:** ![TheMightyRa](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@TheMightyRa](https://forum.freecodecamp.org/u/TheMightyRa)\
**Post date:** [October 14, 2019, 4:30pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/14 "2019-10-14T16:30:20Z")

</div>

You might have to see the lesson because it is asking for at least 4 else statements, at least 4 if statements, and at least one return statement.

---

<div class="post-metadata">

**Author:** ![dlyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dlyons/32/158837_2.png) [@dlyons](https://forum.freecodecamp.org/u/dlyons)\
**Post date:** [October 14, 2019, 4:32pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/15 "2019-10-14T16:32:02Z")

</div>

hmmm yeah, could you post a link to the challenge?

Ah I found it. Okay it looks like you’re not using the proper values for your conditionals, ie:

`num < 5` - return “Tiny”  
`num < 10` - return “Small”  
`num < 15` - return “Medium”  
`num < 20` - return “Large”  
`num >= 20` - return “Huge”

Take what you’ve got going there and add “else if” statements for each of those between the sizes of Tine and Huge (those two meanwhile are gonna be if and else respectively).

If you’re still getting stuck after that, post what you have and I’ll take a look at it

---

<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:** [January 17, 2021, 8:01pm UTC](https://forum.freecodecamp.org/t/if-else-chains-basic-js/321676/16 "2021-01-17T20:01:06Z")

</div>

Closed on January 17, 2021, 8:01pm UTC


