Forum Discussion

maxtester's avatar
maxtester
Contributor
9 years ago

Datatype number with value one is the same as boolean true??

Hi all,

 

I Have a question regarding the datatypes. In my script I have a function that returns boolean true or a number. Like this:

 

//Returns a true or a number

var Return = function1()

 

//check  return value

if(Return != true){

...

}

else{

...

}

 

So if the result value is true the else part is executed. When the return value is > 1 the if part is executed. So far so good. When the return value is 1 the else part is executed. I do not understand the problem. I also check the type of the retun value which is also a number when 1 is executed. Can anybody help me?

  • Don't think thats gonna work.

     

    true/false conditions don't work with number AFAK. If you are returning two possible types, I suspect you're going to have to check the returned data type first (using 'typeof' or something) and then apply your 'if' statement accordingly.

     

    You're mixing types. Not ideal for long term readability and maintainability.

    • Colin_McCrae's avatar
      Colin_McCrae
      Community Hero

      Well there you go.

       

      I stand (well, sit) corrected.

       

      Just tried it and you're right. 1 is treated as true. And works fine for me.

       

      I shall depart the thread now .... :manhappy:

       

      (I'm still not a fan of mixing types ..... :mantongue: )

      • Colin_McCrae's avatar
        Colin_McCrae
        Community Hero

        My excuse here is my VBScript background.

         

        1 is not treated this way in VB (with it's loose typing). Any number is false. (Well, it's not. It's a number. But it's not true ...)

         

        But in C# script, you are quite right. It is treated as a true. (My current project is my first one using C# script ...)

  • Thank you for the response. As I read from the documentation of TC the idea behind the var datatype is to handle different datatypes. But however. I make a chekc with typeof and the @"1" is always a number and it works for each number except 1. So basicly it works. 

    • Colin_McCrae's avatar
      Colin_McCrae
      Community Hero

      *** EDIT AS MY INITIAL REPLY WAS UTTER RUBBISH ..... IN JSCRIPT/C# SCRIPT ANYWAY ***

       

      The reasoning below however, IS applicable in VBScript ....

       

      1 is number. It is not, and never will be, true or false. As it's not a boolean. Which is why ALL your numbers are dropping into the 'else' part. As they are numbers, not trues. Same as 1 is not the same as '1'. First is a number. Second is a string.

       

      -------------------------------------------------------------------------

       

      Helen's reply below this is 100% correct. :)

  • Thank you all for the help. Now I check before if return value is 1. Then I set the boolean as true. Not nice but it works

    • dganov's avatar
      dganov
      Staff

      Also as I just tested the check

       

      if (!Return) ...

      works as expected (zero value is treated as false, all other values as true).

       

      UPDATE: the reason is that in case (Return != true) JS-engine tries to compare two numbers (true is converted to 1), while in case (!Return) we tell JS-engine to convert value of Return to boolean type.