SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Casting a string to boolean

    Lets say that we have a String with the value "false" or "true". Lets now cast this String to a boolean value. In Java it would be
    Code Java:
    Boolean fun = new Boolean("true");

    JavaScript also provides a Boolean Wrapper object, but it doesn't really give the expected results
    Code JavaScript:
    var notFun = Boolean("false"); // omitting new, see [url]http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Boolean[/url]
    // notFun = true

    The above will only be false if false, NaN, null, 0 or an empty String is provided, everything else will return true.

    So the only way to cast a String with the values "true" or "false" is to use eval...
    Code JavaScript:
    var snowing = eval("false");

    Anybody has another way to do the cast?

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    Anybody has another way to do the cast?
    Code JavaScript:
    var stringRepresentation = "false";
    var booleanRepresentation = (stringRepresentation == "true");

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, yes, thats one way to do it, buy not really a cast. Its a result from a comparison. That is what I wanted to avoid.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    That is what I wanted to avoid.
    Why?

    Quote Originally Posted by Pepejeria View Post
    Thanks, yes, thats one way to do it, buy not really a cast.
    The thing is; What you want to do is not really a type cast. You want to interpret the value of the variable -- not simply change the type of it.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Why?
    I am actually doing that solution now. But it felt somehow cheap compared to what Java offers. It is a pity that the Boolean wrapper object doesn't work like it does in Java. But hey, I know, JavaScript is not Java. I wish it would be more like Java and less like Python, which it is becoming lately (JavaScript 1.7 and the upcoming 1.8).

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    I am actually doing that solution now. But it felt somehow cheap compared to what Java offers. It is a pity that the Boolean wrapper object doesn't work like it does in Java. But hey, I know, JavaScript is not Java. I wish it would be more like Java and less like Python, which it is becoming lately (JavaScript 1.7 and the upcoming 1.8).
    Technically, what java does is not a typecast, but the Boolean(Stirng) constructor, that arbitrarily interprets the string "true" as boolean true. Nice? I find this kind of code rather stinky

    Code:
    Boolean a = new Boolean("true"); // true
    Boolean b = new Boolean("true "); // FALSE

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    String.prototype.reval=function(){
    return eval(this);
    }
    This does not change the string, but returns the evaled value of the string.

    If you hate eval you can define all the values-

    String.prototype.reval=function(){
    if(parseFloat(this)+''===this)return parseFloat(this);
    if(this==='false') return false;
    //etc.
    }

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    String.prototype.reval=function(){
    return eval(this);
    }
    This does not change the string, but returns the evaled value of the string.

    If you hate eval you can define all the values-

    String.prototype.reval=function(){
    if(parseFloat(this)+''===this)return parseFloat(this);
    if(this==='false') return false;
    //etc.
    }
    No, I don't hate eval, I use it as the last resort. Extending native objects would though be more something that I dislike.

  9. #9
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess that it calls parseBoolean internally, but that is another story.

    Anyway, thanks for the answers everybody



Bookmarks

Posting Permissions

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