# If-Else Statement

**URL:** <https://forum.codewithmosh.com/t/if-else-statement/18466>\
**Category:** JavaScript\
**Created:** [February 28, 2023, 2:32pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466 "2023-02-28T14:32:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sterlz08](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sterlz08](https://forum.codewithmosh.com/u/sterlz08)\
**Post date:** [February 28, 2023, 2:32pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466/1 "2023-02-28T14:32:24Z")

</div>

I am currently taking the Javascript course. I’m doing exercise 2 of the array section. I have been having trouble with when to use brackets vs when not to etc. And currently I’m getting caught up on an if-else statement. For some reason my code works when I drop the “else” in my if-else statement. Any ideas/explanation?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/7/7eb0aff1fad2e431a3067954e40b7f0f33293122.png)

---

<div class="post-metadata">

**Author:** ![sterlz08](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sterlz08](https://forum.codewithmosh.com/u/sterlz08)\
**Post date:** [February 28, 2023, 2:33pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466/2 "2023-02-28T14:33:02Z")

</div>

When I add the “else” it doesn’t work

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/3/345a6abf99c9917300e313d4c5052cefe0ab578d.png)

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [February 28, 2023, 5:35pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466/3 "2023-02-28T17:35:56Z")

</div>

**Your Below code In First Image**

```auto
for (let n of array) {
    if (n === value) {
        // your code
    }
    {
        // this block of code will never executed 
        console.log(your_message)
    }
}

```

**IS SAME AS**

```auto
for (let n of array) {
    if (n === value) {
        // your code
    }
}

```

---

<div class="post-metadata">

**Author:** ![sterlz08](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sterlz08](https://forum.codewithmosh.com/u/sterlz08)\
**Post date:** [February 28, 2023, 6:01pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466/4 "2023-02-28T18:01:24Z")

</div>

yes, understood. But why doesn’t it work when the “else” word is added?

_see first reply screenshot with red outlines_

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [February 28, 2023, 8:45pm UTC](https://forum.codewithmosh.com/t/if-else-statement/18466/5 "2023-02-28T20:45:59Z")

</div>

> [@AzharUddinSheikh](#):
>
> `for (let n of array) {`

Pretty much. You instinctively added the braces for the if which are not in the original code. But the explanation is correct. When you do not delimit an `if` with curly braces, it deals with the next statement only. It happens that the next statement has a body so multiple lines are executed.

Now for the other part it is just like the curly braces around the `console.log` are not there.

When you add the `else` you go through your array value by value. So you go with -2 first which differs from 25. The `return` instruction exits the `for` loop.

When the `else` is not there, it will simply go on until it reaches the value. So it will work as intended without `else` but not with. This could be discouraged to write the code that way.

Ultimately your code is equivalent to this which is OK. There is no more misleading curly braces and the intent is more explicit.

```auto
const numbers = [-2, 5, 9, -8, -10, 10, 11, 25];

let myIncludes = (array, value) => {
    for (let n of array) {
        if (n === value) {
            console.log(value + " is part of this array! :)");
            return true;
        }
    }

    console.log(value + " is not part of this array, sorry! :(");
    return false;
};

console.log(myIncludes(numbers, 25));

```

Many argue not putting curly braces around one-liners `if`, `for`, etc… statements is not a good practice and I wouldn’t agree most of the time. But in this scenario I would put them.

On the other hand I see nothing wrong with something like

```auto
if (condition)
   console.log("I am actually nicer and more readable.");

```

> 💯 **Pro Tip**
> 
> Learn to use the debugger. Either in any IDE, code editor capable of it or a web browser.  
> You can just stop the execution and go step by step to observe and understand the behaviour of your code.
> 
> ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/8/8384a2a0785814b9add2df9285e2f369e1803aa9.png)
