# If/else statement

**URL:** <https://community.postman.com/t/if-else-statement/21163>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [March 9, 2021, 1:34pm UTC](https://community.postman.com/t/if-else-statement/21163 "2021-03-09T13:34:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 9, 2021, 1:34pm UTC](https://community.postman.com/t/if-else-statement/21163/1 "2021-03-09T13:34:11Z")

</div>

Hi everyone,

I set enviroment variable and want to check SID data for local and develop machines. After sending request I get the same result - is checked only else statemant.  
Please, help me understand why if statement is ignored in any case.  
Thanks so much.

 ![pm](https://us1.discourse-cdn.com/getpostman/original/3X/9/7/971baf37aedc4199c24b0666f57654af07d983fa.png)

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [March 9, 2021, 2:51pm UTC](https://community.postman.com/t/if-else-statement/21163/2 "2021-03-09T14:51:14Z")

</div>

Change it to

```
     If(pm.environment.get("environment")==="local")
```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 9, 2021, 3:24pm UTC](https://community.postman.com/t/if-else-statement/21163/3 "2021-03-09T15:24:41Z")

</div>

Hey @harutharut

As well as needing to be:

```auto
if (pm.environment.get('environment') === 'local') {
    // your test code
}

```

The syntax of your `if/else` statement isn’t quite right.

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

```auto
if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

```

To add additional conditions you would need to use `else if`

```auto
if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

```

You could make use of `pm.environment.name` here to check the active environment name, rather that a value within it:

```auto
if(pm.environment.name === 'local') {
  // do something...
} else if (pm.environment.name === 'develop') {
  // do something...
} else {
  // do not do anything...
}

```

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 10, 2021, 6:59am UTC](https://community.postman.com/t/if-else-statement/21163/4 "2021-03-10T06:59:19Z")

</div>

Hey @danny-dainton  
Thank you guy for detailed answer, it helped me 🙂

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 10, 2021, 7:02am UTC](https://community.postman.com/t/if-else-statement/21163/5 "2021-03-10T07:02:16Z")

</div>

Hello @praveendvd  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![ppoorna](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@ppoorna](https://community.postman.com/u/ppoorna)\
**Post date:** [January 10, 2023, 3:25pm UTC](https://community.postman.com/t/if-else-statement/21163/6 "2023-01-10T15:25:58Z")

</div>

Hi Team/ @harutharut ,

Kindly help me out in this one… similarly one issue my … else if part is NOT working… correctly

```auto
let jsonData = pm.response.json();
let contextAction = ["New", "Confirm", "Different"]
//console.log(jsonData.contextAction);

if(jsonData.contextAction === "New") {
   console.log(jsonData.contextAction);
   pm.test(`Verify the ContextAction Value is -> ${jsonData.contextAction}`,()=>{
      pm.expect(jsonData.contextAction).to.be.eqls("New");     
   })
} else if (jsonData.contextAction === "Confirm") {
   pm.test(`Verify the contextAction Value is -> ${jsonData.contextAction}`,()=> {
      pm.expect(jsonData.contextAction).to.be.eqls("Confirm");
      
      //pm.expect(jsonData.contextAction).to.be.oneOf(contextAction)
   })
} else { 
   pm.test(`Verify the contextAction Value is -> ${jsonData.contextAction}`,()=>{
      pm.expect(jsonData.contextAction).to.be.eqls("Different");
      
   })
}

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/1/a13c7e25acf2f42b91726695221e054419192fa8.png)

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [January 10, 2023, 4:17pm UTC](https://community.postman.com/t/if-else-statement/21163/7 "2023-01-10T16:17:13Z")

</div>

Hi @ppoorna ,  
first of all you can check and see that  
`console.log(jsonData.contextAction);`  
the result is `undefined`. It means you can not check in your `if` block undefined condition.  
You should set an variable and then get it in condition.  
[Using variables](https://learning.postman.com/docs/sending-requests/variables/)  
Please check it and look above on my issue and solution.

---

<div class="post-metadata">

**Author:** ![ppoorna](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@ppoorna](https://community.postman.com/u/ppoorna)\
**Post date:** [January 10, 2023, 4:30pm UTC](https://community.postman.com/t/if-else-statement/21163/8 "2023-01-10T16:30:07Z")

</div>

@harutharut well, i understand it, but the issue is not on the variables i did some research that postman is **not supporting the ‘if else if else’ ladder**

---

<div class="post-metadata">

**Author:** ![ppoorna](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@ppoorna](https://community.postman.com/u/ppoorna)\
**Post date:** [January 10, 2023, 4:31pm UTC](https://community.postman.com/t/if-else-statement/21163/9 "2023-01-10T16:31:08Z")

</div>

@harutharut In case if your free pls do connect i will explain it sir. thanks

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 10, 2023, 4:54pm UTC](https://community.postman.com/t/if-else-statement/21163/10 "2023-01-10T16:54:40Z")

</div>

@ppoorna

else if does work

Pretty sure its just because you have a few }) in the wrong places.

If you post code, can you please use the preformatted text option in the editor, so it aligns correctly and is easier to read. (Also easier to see mistakes in code).

```auto
let contextAction = "Confirm";

if (contextAction === "New") {
    console.log("New");
} else if (contextAction === "Confirm") {
    console.log("Confirm")
} else {
    console.log("Different")
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 22, 2023, 2:39pm UTC](https://community.postman.com/t/if-else-statement/21163/11 "2023-08-22T14:39:27Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
