If I declare a JavaScript boolean variable like this:

var IsLoggedIn;

And then initialize it with either true or 1, is that safe? Or will initializing it with 1 make the variable a number?

8

Best Answer


Types are dependent to your initialization:

var IsLoggedIn1 = "true"; //stringvar IsLoggedIn2 = 1; //integervar IsLoggedIn3 = true; //bool

But take a look at this example:

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

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

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); // falsevar IsLoggedIn = new Boolean(NaN); // falsevar IsLoggedIn = new Boolean("Foo"); // truevar IsLoggedIn = new Boolean(1); // true

As this very useful tutorial says:

var age = 0;// badvar hasAge = new Boolean(age);// goodvar hasAge = Boolean(age);// goodvar hasAge = !!age;

If you want IsLoggedIn to be treated as a boolean you should initialize as follows:

var IsLoggedIn=true;

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

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

 IsLoggedIn="Hello World";

This will not cause an error.

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

var iAmNotDefined;alert(!iAmNotDefined); //true//oralert(!!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); //=> truealert(!!thisMayBeTrue); //=> truealert(thisMayBeTrue === true); //=> falsethisMayBeTrue = '1';alert(thisMayBeTrue == true); //=> true alert(!!thisMayBeTrue); //=> truealert(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); //=> truealert(!!thisMayBeTrue); //=> truealert(thisMayBeTrue === true); //=> truethisMayBeTrue = 'true';alert(thisMayBeTrue == true); //=> falsealert(!!thisMayBeTrue); //=> truealert(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;~)

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.

How about something like this:

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

Then you can use it like this:

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

I have not tested it for performance, but converting from type to type should not happen too often otherwise you open your app up to instability big time!

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.