# Syntax for multiple level if then statement

**URL:** <https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802>\
**Category:** General\
**Created:** [4 December 2019 23:07 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802 "2019-12-04T23:07:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raj](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Raj](https://discourse.nodered.org/u/Raj)\
**Post date:** [4 December 2019 23:07 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/1 "2019-12-04T23:07:13Z")

</div>

Hi,

Not sure what I am doing wrong, can't seem to get the right syntax.

I have a first IF loop when that works, then it drops into a second and third IF loop.

But when the first IF is not valid, it drops into an ELSE condition and continues processing from there.

```auto
a = 200;
var status = "status";

if (a== 100){
    b = 200;
    if (b = 200);
        c=300;
        if (c = 300);
            status = "first";
        
}else if (a != 100) {
        b = 200;
    if (b = 200);
        c=300;
        if (c = 300);
            status = "second";
}      

return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [4 December 2019 23:10 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/2 "2019-12-04T23:10:32Z")

</div>

What did you expect to happen?

---

<div class="post-metadata">

**Author:** ![Raj](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Raj](https://discourse.nodered.org/u/Raj)\
**Post date:** [4 December 2019 23:14 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/3 "2019-12-04T23:14:46Z")

</div>

Trying to get the second and third if statements to work.

To process from the first section if a = 200.

If A is not 200 expect it to drop to the else then process from there.

This is not the code I am working with, I am just trying to get the syntax correct so I can integrate it into my code

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [4 December 2019 23:30 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/4 "2019-12-04T23:30:40Z")

</div>

But if you simplify your code -

```auto
if (a== 100){
       do something        
       }
   else if (a != 100) {
             do something else;
             }      

```

You can see why it's failing

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 December 2019 23:41 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/5 "2019-12-04T23:41:18Z")

</div>

Be careful to use `==` or `===` in your `if` statements and **not** `=` by itself. The former are used to compare values, the latter assigned the value.

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [5 December 2019 02:54 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/6 "2019-12-05T02:54:44Z")

</div>

There's a few problems with your code...

- As @knolleary points out, you're using `=` in some if statements, which is assignment, when you should be using `==` or `===`.

- Some of your if statements end in a `;`, which ends them. The statement on the next line is therefore not part of the if, and will always execute.

- You have indented some of the code as if it should be part of a block. Python works this way; Javascript does not. For Javascript you need to define a block using`{...}` as you have done for the `if (a == 100) {...` The test for `b` and `c` are therefore within the test for `a`, but `c` is not within `b`.

- The `else if` can just be simplified to `else` given if `a` is 100, the else condition is `a` is not 100.

- You also said  
"But when the first IF is not valid, it drops into an ELSE condition and continues processing from there"  
This is what is supposed to happen. Everything within the `{...}` after the first `if` is treated as one block. If the `if` fails, the `else` (if it exists) is executed.

Your code should look more like that below. Assuming that's what you want to achieve, because I'm guessing it's just made up for the purpose of posting as it does nothing useful.

```auto
if (a === 100) {
    b = 200;
    if (b === 200) {
        c = 300;
        if (c === 300)
            status = "first";
    }
} else {
    b = 200;
    if (b === 200) {
        c = 300;
        if (c === 300)
            status = "second";
    }
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2019 09:03 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/7 "2019-12-05T09:03:37Z")

</div>

My recommendation, particularly for beginners, is _always_ to use {..} for if statements and blocks, even for single line blocks. It makes the code much less prone to the sort of errors you have in your code.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 December 2019 09:26 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/8 "2019-12-05T09:26:42Z")

</div>

```auto
if (a === 100) {
    b = 200;
    if (b === 200) {
...

```

In other words, if a == 100 then b is always 200 and c is always 300.

---

<div class="post-metadata">

**Author:** ![Raj](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Raj](https://discourse.nodered.org/u/Raj)\
**Post date:** [6 December 2019 18:57 UTC](https://discourse.nodered.org/t/syntax-for-multiple-level-if-then-statement/18802/9 "2019-12-06T18:57:24Z")

</div>

Hi Guys,

Thank you all for helping, it turned out to be a mismatch of =, == and ===.

I also learnt more about writing JavaScript code along the way.

That part of my code is functional, just working on the other bugs, lol.

Merry X-mas.
