# JS question for If statement

**URL:** <https://forum.freecodecamp.org/t/js-question-for-if-statement/133700>\
**Category:** General\
**Tags:** javascript\
**Created:** [July 3, 2017, 7:34am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700 "2017-07-03T07:34:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 3, 2017, 7:34am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/1 "2017-07-03T07:34:29Z")

</div>

What is the different between using IF statement alone and using else with it?

---

<div class="post-metadata">

**Author:** ![vikramnr](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vikramnr/32/34785_2.png) [@vikramnr](https://forum.freecodecamp.org/u/vikramnr)\
**Post date:** [July 3, 2017, 7:37am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/2 "2017-07-03T07:37:42Z")

</div>

Using Else with if gives a alternative option in case if the condition fails. I’m not clear what do you want out your question

---

<div class="post-metadata">

**Author:** ![owel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/owel/32/48425_2.png) [@owel](https://forum.freecodecamp.org/u/owel)\
**Post date:** [July 3, 2017, 8:09am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/3 "2017-07-03T08:09:04Z")

</div>

In a flow chart, it looks like this:

![](https://global.discourse-cdn.com/freecodecamp/original/3X/a/d/ad1509827e64bba02455424f5ed5a78398026aca.gif)

---

<div class="post-metadata">

**Author:** ![imnotjohn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/imnotjohn/32/60697_2.png) [@imnotjohn](https://forum.freecodecamp.org/u/imnotjohn)\
**Post date:** [July 3, 2017, 8:43am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/4 "2017-07-03T08:43:47Z")

</div>

If you only have a single ‘if’ statement w/o an accompanying ‘else’ statement, then the code within this lone ‘if’ statement will execute if and only if the statement evaluates as True. If, instead, you have it accompanied by an ‘else’ statement, then should the initial ‘if’ statement fail, it will automatically execute the code enclosed in the ‘else’ statement.

**Scenario 1:**  
`if (test) {`  
`/* will execute if test evaluates as true */`  
`}`  
`/*code outside the if statement will execute whether or not the initial if statement evaluates as true*/`

**Scenario 2:**  
`if (test 1) {`  
`/* will execute test1 evaluates as true */`  
`} else {`  
`/* will execute if and only if test1 evaluates as false */`  
`}`

**Scenario 3**  
`if(test1) {`  
`/* will execute test1 evaluates as true */`  
`} else if (test2) {`  
`/* will execute test2 evaluates as true AND test1 has evaluated as false */`  
`} else {`  
`/* will execute if and only if test1 and test2 evaluates as false */`  
`}`

Does that help at all?

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 4, 2017, 3:23am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/5 "2017-07-04T03:23:45Z")

</div>

What I mean, can I use IF statement without adding else to it or not (( as happened in the Logical challenge where if mentioned first time )) ?

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 4, 2017, 3:24am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/6 "2017-07-04T03:24:30Z")

</div>

Thank you for helping \<3

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 4, 2017, 3:28am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/7 "2017-07-04T03:28:21Z")

</div>

Well, you seem to be the nearest one to understand what I mean. What I asked for what is the difference between scenario 1 and scenario 2 in your answer? I am not sure and correct me if I wrong - without else the code block out of the if parentses will excute even the if condition is true, but with ELSE it will excute only if the condition was wrong - is that right ?

---

<div class="post-metadata">

**Author:** ![vikramnr](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vikramnr/32/34785_2.png) [@vikramnr](https://forum.freecodecamp.org/u/vikramnr)\
**Post date:** [July 4, 2017, 4:36am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/8 "2017-07-04T04:36:41Z")

</div>

Yes you can absolutely use If without an Else statement.

---

<div class="post-metadata">

**Author:** ![imnotjohn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/imnotjohn/32/60697_2.png) [@imnotjohn](https://forum.freecodecamp.org/u/imnotjohn)\
**Post date:** [July 4, 2017, 8:55pm UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/9 "2017-07-04T20:55:39Z")

</div>

Without an ‘else’, any code outside of an ‘if’ statement will execute as long as the ‘if’ doesn’t contain a ‘return’ etc.

yes, the code within an ‘else’ will execute if and only if the initial ‘if’ condition evaluates as false. but if there is a block of code outside of the ‘else’, it will also still execute.

`if (test1) {`  
`/* executes if and only if test1 evaluates as True */`  
`} else {`  
`/* executes if and only if test1 evaluates as False */`  
`}`  
`/* code here will still execute once the if/else has finished being evaluated */`

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 5, 2017, 5:02am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/10 "2017-07-05T05:02:33Z")

</div>

Well, that’s more clear now. Thank you for your help, dude 🙂

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 5, 2017, 5:03am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/11 "2017-07-05T05:03:27Z")

</div>

I got it. Thank you man 🙂

---

<div class="post-metadata">

**Author:** ![imnotjohn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/imnotjohn/32/60697_2.png) [@imnotjohn](https://forum.freecodecamp.org/u/imnotjohn)\
**Post date:** [July 6, 2017, 4:24am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/12 "2017-07-06T04:24:10Z")

</div>

nice. good luck!

one thing i do when i have trouble understanding how / why code is working a certain way is to do something like this:

if i wanted to understand what was happening w/ the if / else thing, I would just try to write test code for the different scenarios i’m curious about.

```
var str = '';

function() { 
  if (test1) {
    str = 'within if';
  } else {
    str = 'within else';
  }
  str = 'outside if';

  return str;
}

```

then call the function() to see how the output changes depending on whether or not test1 evaluates as True or False. you can play around w/ it and explore multiple scenarios this way. lmk whether or not that makes sense.

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 6, 2017, 4:26am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/13 "2017-07-06T04:26:33Z")

</div>

Brilliant. I will remember this way for coming problems.

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [July 6, 2017, 4:29pm UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/14 "2017-07-06T16:29:57Z")

</div>

`if (condition) {do something} else {do something else}` is the same as `if (condition) {do something}; if (!condition) {do something else}`. In other words, it’s like “if not”, which works the same as in natural language:

If there’s bread, buy one loaf. If not, go to the next store.

```auto
var storeIdx = 0;
while (me.loaves === 0 && stores[storeIdx]) {
  if (stores[storeIdx].loaves >= 1) {
    me.money -= 1.00;
    stores[storeIdx].money += 1.00;
    stores[storeIdx].loaves -= 1;
    me.loaves += 1;
  } else {
    storeIdx++;
  }
}

```

If the door is open, go straight in. If not, knock first.

```auto
if (doorIsOpen) {
  goStraightIn();
} else {
  knockOnDoor();
}

```

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 7, 2017, 5:44am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/15 "2017-07-07T05:44:20Z")

</div>

Thank you for helping 🙂

---

<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, 9:37am UTC](https://forum.freecodecamp.org/t/js-question-for-if-statement/133700/16 "2021-01-17T09:37:10Z")

</div>

Closed on January 17, 2021, 9:37am UTC


