0

I am calling a JS function from vb.net code behind, using RegisterClientScriptBlock

Initially this variable is a string having value as (true/false), then I parse it using custom code to booelan and I am passing this Boolean value as one of the parameter to a JavaScript function. Inside this JS function I am checking if that bool value is true or not using the below code. Now even though the bool value is false, the if(boolVariable) line of code is executing.

If I do an explicit check if(boolVariable == true) then it behaves as expected.

My understanding is that if(boolvariable) block should execute only if the boolVariable value is true. I am missing something ? Please guide.

Vb.net code

Dim jsCall As String = String.Format("jsfunction('{0}','{1}');", variable1, CustomParsers.Boolean(boolVariable)) // custom parser value will be true or false

Page.ClientScript.RegisterClientScriptBlock([GetType](), "script", jsCall, True)

JS code

function jsfunction(variable1,boolVariable) {
       
     if (boolVariable) {  // though boolVariable value is false this block is executing
          // do something
        }
        
}
Sannn
  • 45
  • 7
  • Are you certain that `boolVariable` is a boolean (and not a string)? What is outputted if you were to add `console.log(typeof boolVariable);`? `"false" != true`, but `"false"` is truthy, which is why I'm asking this. – theusaf May 31 '21 at 04:25
  • 1
    `jsfunction('{0}','{1}');` seems suspiciously like you're passing strings. – Niet the Dark Absol May 31 '21 at 04:28
  • @theusaf - Yes you were right, console shows - type of:string , though I pass boolean Then how do I check it ? – Sannn May 31 '21 at 05:29
  • @Nietthedarkabsol - jsfunction('{0}','{1}'); When I give without quotes like jsfunction('{0}',{1}) - It throes an error saying False is undefined. – Sannn May 31 '21 at 05:29
  • In JavaScript, `False/True` are lowercase: `true`/`false` – theusaf May 31 '21 at 23:25

0 Answers0