Declaring a boolean in JavaScript using just var

后端 未结 8 1543
余生分开走
余生分开走 2020-12-23 02:54

If I declare a JavaScript boolean variable like this:

var IsLoggedIn;

And then initialize it with either true or 1

相关标签:
8条回答
  • 2020-12-23 03:16

    No it is not safe. You could later do var IsLoggedIn = "Foo"; and JavaScript will not throw an error.

    It is possible to do

    var IsLoggedIn = new Boolean(false);
    var IsLoggedIn = new Boolean(true);
    

    You can also pass the non boolean variable into the new Boolean() and it will make IsLoggedIn boolean.

    var IsLoggedIn = new Boolean(0); // false
    var IsLoggedIn = new Boolean(NaN); // false
    var IsLoggedIn = new Boolean("Foo"); // true
    var IsLoggedIn = new Boolean(1); // true
    
    0 讨论(0)
  • 2020-12-23 03:19

    Variables in Javascript don't have a type. Non-zero, non-null, non-empty and true are "true". Zero, null, undefined, empty string and false are "false".

    There's a Boolean type though, as are literals true and false.

    0 讨论(0)
  • 2020-12-23 03:22

    You can use and test uninitialized variables at least for their 'definedness'. Like this:

    var iAmNotDefined;
    alert(!iAmNotDefined); //true
    //or
    alert(!!iAmNotDefined); //false
    

    Furthermore, there are many possibilites: if you're not interested in exact types use the '==' operator (or ![variable] / !![variable]) for comparison (that is what Douglas Crockford calls 'truthy' or 'falsy' I think). In that case assigning true or 1 or '1' to the unitialized variable always returns true when asked. Otherwise [if you need type safe comparison] use '===' for comparison.

    var thisMayBeTrue;
    
    thisMayBeTrue = 1;
    alert(thisMayBeTrue == true); //=> true
    alert(!!thisMayBeTrue); //=> true
    alert(thisMayBeTrue === true); //=> false
    
    thisMayBeTrue = '1';
    alert(thisMayBeTrue == true); //=> true 
    alert(!!thisMayBeTrue); //=> true
    alert(thisMayBeTrue === true); //=> false
    // so, in this case, using == or !! '1' is implicitly 
    // converted to 1 and 1 is implicitly converted to true)
    
    thisMayBeTrue = true;
    alert(thisMayBeTrue == true); //=> true
    alert(!!thisMayBeTrue); //=> true
    alert(thisMayBeTrue === true); //=> true
    
    thisMayBeTrue = 'true';
    alert(thisMayBeTrue == true); //=> false
    alert(!!thisMayBeTrue); //=> true
    alert(thisMayBeTrue === true); //=> false
    // so, here's no implicit conversion of the string 'true'
    // it's also a demonstration of the fact that the 
    // ! or !! operator tests the 'definedness' of a variable.
    

    PS: you can't test 'definedness' for nonexisting variables though. So:

    alert(!!HelloWorld);
    

    gives a reference Error ('HelloWorld is not defined')

    (is there a better word for 'definedness'? Pardon my dutch anyway;~)

    0 讨论(0)
  • 2020-12-23 03:22

    The variable will become what ever type you assign it. Initially it is undefined. If you assign it 'true' it will become a string, if you assign it true it will become a boolean, if you assign it 1 it will become a number. Subsequent assignments may change the type of the variable later.

    0 讨论(0)
  • 2020-12-23 03:29

    As this very useful tutorial says:

    var age = 0;
    
    // bad
    var hasAge = new Boolean(age);
    
    // good
    var hasAge = Boolean(age);
    
    // good
    var hasAge = !!age;
    
    0 讨论(0)
  • 2020-12-23 03:30

    Types are dependent to your initialization:

    var IsLoggedIn1 = "true"; //string
    var IsLoggedIn2 = 1; //integer
    var IsLoggedIn3 = true; //bool
    

    But take a look at this example:

    var IsLoggedIn1 = "true"; //string
    IsLoggedIn1 = true; //now your variable is a boolean
    

    Your variables' type depends on the assigned value in JavaScript.

    0 讨论(0)
提交回复
热议问题