# Creating a if statement inside an if statement in a Javascript function node

**URL:** <https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663>\
**Category:** Developing Nodes\
**Tags:** function-node\
**Created:** [7 October 2022 08:34 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663 "2022-10-07T08:34:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Breathly](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@Breathly](https://discourse.nodered.org/u/Breathly)\
**Post date:** [7 October 2022 08:34 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/1 "2022-10-07T08:34:32Z")

</div>

Hey guys, currently i have some problems with not knowing how to create a if satement inside a if statement in a Javascript function node, currently last piece i need to do for my project to work. How do i do this? Is it even possible?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 08:44 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/2 "2022-10-07T08:44:23Z")

</div>

Have you looked at examples?  
[nested examples](https://www.google.com/search?q=Nested+if+JavaScript+examples&client=ms-unknown&biw=1280&bih=720&ei=Oeo_Y8DBJuuM9u8PlKeL6As&oq=Nested+if+JavaScript+examples&gs_lcp=ChNtb2JpbGUtZ3dzLXdpei1zZXJwEAMyBggAEB4QBzIGCAAQHhAIMgUIABCGAzIFCAAQhgMyBQgAEIYDMgUIABCiBDIHCAAQHhCiBDIHCAAQHhCiBDoKCAAQRxDWBBCwAzoICAAQHhAIEAc6BAghEApKBAhBGABQrRJYjCFg7iNoAXAAeACAAcUBiAHUA5IBAzAuM5gBAKABAcgBCMABAQ&sclient=mobile-gws-wiz-serp#ip=1&sbfbu=1&pi=Nested%20if%20JavaScript%20examples)

[edit] fixed wrong link

---

<div class="post-metadata">

**Author:** ![Breathly](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@Breathly](https://discourse.nodered.org/u/Breathly)\
**Post date:** [7 October 2022 08:46 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/3 "2022-10-07T08:46:23Z")

</div>

I have not. I will thank you!

---

<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:** [7 October 2022 08:55 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/4 "2022-10-07T08:55:46Z")

</div>

Actually that example page is for Go rather than Javascript, though the principle is the same

```auto
if (A) {
  ...
  if (B) {
    ...
  }
  ...
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 09:03 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/5 "2022-10-07T09:03:27Z")

</div>

Indeed it is the same principle.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 October 2022 10:17 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/6 "2022-10-07T10:17:51Z")

</div>

Internet search should always be the first port of call:

- [5 Tips to Write Better Conditionals in JavaScript | DigitalOcean](https://www.digitalocean.com/community/posts/5-tips-to-write-better-conditionals-in-javascript)
- [JavaScript Best Practices — Conditionals | by John Au-Yeung | Level Up Coding (gitconnected.com)](https://levelup.gitconnected.com/javascript-best-practices-conditionals-f1dd694b8f9e)
- [Best Practices for writing If-else. | by Bal sikandar | Medium](https://medium.com/@balsikandar/best-practices-for-writing-if-else-7e89dd503754)
- [if...else - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)

---

<div class="post-metadata">

**Author:** ![khanzain](https://avatars.discourse-cdn.com/v4/letter/k/df705f/32.png) [@khanzain](https://discourse.nodered.org/u/khanzain)\
**Post date:** [19 February 2024 06:42 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/7 "2024-02-19T06:42:14Z")

</div>

Yes, it is indeed possible to nest if statements within other if statements in [JavaScript](https://iqratechnology.com/academy/javascript-training/) functions. This technique is commonly used to create more complex conditional logic. Here's a basic example:

function myFunction(condition1, condition2) {  
if (condition1) {  
// Executed if condition1 is true  
if (condition2) {  
// Executed if both condition1 and condition2 are true  
console.log("Both conditions are true.");  
} else {  
// Executed if condition1 is true but condition2 is false  
console.log("Condition1 is true, but Condition2 is false.");  
}  
} else {  
// Executed if condition1 is false  
console.log("Condition1 is false.");  
}  
}  
In this example, we have a function myFunction that takes two conditions as parameters. Inside the function, we have nested if statements to handle different scenarios based on the conditions provided. Depending on the values of condition1 and condition2, different blocks of code will be executed.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 February 2024 07:00 UTC](https://discourse.nodered.org/t/creating-a-if-statement-inside-an-if-statement-in-a-javascript-function-node/68663/8 "2024-02-19T07:00:11Z")

</div>


