Regarding Script

Astik Thombare
Tera Sage

Hi Community ,

 

var a = true;
if (a == "true") {
gs.info("inside if");
} else {
gs.info("inside else");
}

 

Need to know why its output is coming as - inside else

 

Can someone explain it to me in simple words ?

 

Thanks in Advance

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

You are declaring 'a' with a boolean value and testing with a string value.  Boolean != string.

Sandeep Rajput
Tera Patron
Tera Patron

@Astik Thombare You are comparing a boolean value true with a string value "true" this check is bound to fail hence the else part is executing.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Astik Thombare ,
I trust you are doing great.

To fix this and make the if block execute, you could either:

  • Change the condition to compare with the Boolean true: if (a === true)
  • Or, use the strict equality operator === which does not perform type coercion: if (a === "true") (but in this case, you would also need to change the value of a to a string: var a = "true";)

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi