I have a few conversion/cast questions. From a book I am reading:
At the console (firebug), why do the following give the results below
!!"false" results in true;
var v = v || 10; results in undefined;
"false" is a String not a Boolean, because you put it inside quotes. A string is always evaluated as a Boolean true, thus !!"false" is the same as !!true, which, obviously is evaluated as true.
Only should have been !!false, the expression would be evaluated as false .
That is not true. var v=v||10;, the result (the result of the assignment) is that the variable v gets the value 10.
Keep in mind that the second v is taken as being another, independent variable. Could be an argument passed to a function. Or a global variable, if the assignment is performed inside a function. Or, if the code is written outside a function (thus it have a global scope)the variable v is not yet defined the moment you try the assignment.
alert(v); // alerts 10
Last edited by Kor; 03-31-2011 at 09:26 AM.
I think I understand the 2nd one now i.e. var v = v || 10;
What you effectively have is var v = undefined || 10 as v has not been assigned a value at all yet.
And undefined || 10 evaluates to 10.
Am I on the right track there?
Yes, you are right. That assignment works like an if-else statement
Originally Posted by onefootswill
Awesome. Thanks very much!
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)