www.webdeveloper.com
Results 1 to 5 of 5

Thread: Conversion Questions

  1. #1
    Join Date
    Jan 2009
    Posts
    17

    Conversion Questions

    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;

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    1.
    "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 .

    2.
    That is not true. var v=v||10;, the result (the result of the assignment) is that the variable v gets the value 10.
    Code:
    var v=v||10;
    alert(v); // alerts 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.
    Last edited by Kor; 03-31-2011 at 10:26 AM.

  3. #3
    Join Date
    Jan 2009
    Posts
    17
    Thanks KOR.

    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?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by onefootswill View Post
    Thanks KOR.

    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
    Code:
    if(v){
    var v=v;
    }
    else{
    var v=10;
    }

  5. #5
    Join Date
    Jan 2009
    Posts
    17
    Awesome. Thanks very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles