JavaScript boolean declaration using only var

If I declare a JavaScript boolean variable like this:

var IsLoggedIn; 

And then initialize it with true or 1 , is it safe? Or initialize it with 1 to make the variable a number?

+73
javascript boolean
Mar 17 '09 at 11:58
source share
8 answers

Types depend on your initialization:

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

But look at this example:

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

The type of your variables depends on the assigned value in JavaScript.

+140
Mar 17 '09 at 12:03
source share

No, this is not safe. You can later run var IsLoggedIn = "Foo"; , and JavaScript will not throw an error.

Can do

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

You can also pass a non-boolean variable to new Boolean() , and this will make the IsLoggedIn logic logical.

 var IsLoggedIn = new Boolean(0); // false var IsLoggedIn = new Boolean(NaN); // false var IsLoggedIn = new Boolean("Foo"); // true var IsLoggedIn = new Boolean(1); // true 
+19
Mar 17 '09 at 12:00
source share

If you want IsLoggedIn to IsLoggedIn considered logical, you should initialize as follows:

 var IsLoggedIn=true; 

If you initialize it with var IsLoggedIn=1; then it will be considered as an integer.

However, at any time, the IsLoggedIn variable may refer to a different data type:

  IsLoggedIn="Hello World"; 

This will not result in an error.

+7
Mar 17 '09 at 12:01
source share

As this very useful tutorial says:

 var age = 0; // bad var hasAge = new Boolean(age); // good var hasAge = Boolean(age); // good var hasAge = !!age; 
+5
Mar 26 '13 at 13:35
source share

You can use and test uninitialized variables, at least for your "certainty." Like this:

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

In addition, there are many possibilities: if you are not interested in the exact types, use the '==' operator (or! [Variable] / !! [variable]) to compare (this is what Douglas Crockford calls โ€œtrueโ€ or โ€œfalseโ€ ', I think). In this case, assigning true or 1 or '1' for a unified variable always returns true when prompted. Otherwise [if you need safe type comparisons] 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 no implicit conversion of the string 'true' // it also a demonstration of the fact that the // ! or !! operator tests the 'definedness' of a variable. 

PS: you cannot check "certainty" for non-existent variables. So:

 alert(!!HelloWorld); 

gives an Error link ("HelloWorld undefined")

(is there a better word for โ€œdefinitenessโ€?) I apologize for my Dutch language;)

+4
Mar 17 '09 at 13:06
source share

Variables in Javascript have no type. Nonzero, nonempty, nonempty, and true are "true." Zero, null, undefined, empty string, and false are false.

There is a boolean here, as are the literals true and false .

+2
Mar 17 '09 at 12:06
source share

The variable will become the type that you assigned to it. It is initially undefined . If you set it to 'true' , it will become a string, if you assign it to true , it will become logical, if you assign it to 1 , it will become a number. Subsequent assignments may later change the type of variable.

+1
Mar 17 '09 at 12:05
source share

How about something like this:

 var MyNamespace = { convertToBoolean: function (value) { //VALIDATE INPUT if (typeof value === 'undefined' || value === null) return false; //DETERMINE BOOLEAN VALUE FROM STRING if (typeof value === 'string') { switch (value.toLowerCase()) { case 'true': case 'yes': case '1': return true; case 'false': case 'no': case '0': return false; } } //RETURN DEFAULT HANDLER return Boolean(value); } }; 

Then you can use it as follows:

 MyNamespace.convertToBoolean('true') //true MyNamespace.convertToBoolean('no') //false MyNamespace.convertToBoolean('1') //true MyNamespace.convertToBoolean(0) //false 

I have not tested it for performance, but conversion from type to type should not happen too often, otherwise you will quickly open the application to instability!

-one
Jun 20 2018-12-18T00:
source share



All Articles