Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    Difference between return 0 and return false

    On a page, I had <body onhelp="window.location='../help.htm';return false">

    It worked fine. But why is it that when I put return 0 it doesn't hide the help menu? the boolean integers have always worked for me in functions, why don't they work now?

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because zero and false have different purposes, as they are different things. In a condition statement (using == ), zero and false are treated as the same thing. But if you use ===, zero and false are not treated as the same. Sometimes you need to find zero without being false.

    Using return 0, will return zero, which is a number. Using return false says that there is nothing to return, or don't return.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    I've used functions like this all the time, and it works for true and 1. And even using 0 or false (or the !not operator) works with it.

    function c(X)
    {
    if(document.forms[0][X].checked){return 1}
    }

    I've never encountered a triple === before. I think it's a PHP thing, but what's the logic of it? Does it apply to javascript?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In condition statements, true/1 or false/0 is treated as the same, but in event handlers, you need to return a real boolean true/false (not 1/0) to (usually) suppress the event or not.

    <a href="page.htm" onclick="return false">Unclickable link</a>
    <body oncontextmenu="alert('No context menu');return false">

    You can do a return 1/0 in a function if the output of that function will be used as a conditional statement.

    function c(X)
    {
    if(document.forms[0][X].checked) return 1;
    else return 0;
    }

    if (c(0)) // 1/0 is typecasted as true/false internally
    alert("The first element is checked.");
    else
    alert("The first element is not checked.");

    I don't know if I explained it well. But I hope that clears things up a bit.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    Makes perfect sense to me. Functions I make can accept 1 and 0, but internal functions or events require the real true/false deal.
    Thanks

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    === means strict equality:

    ("3" == 3) // true
    ("3" === 3) // false
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    in statements like 0:1 the real meaning is yes/no, which are not strictly boolean operators. There is a difference between comparation ( 0:1 ) in an if statement) and existence (true/false) in a return function. Existence is a strictly boolean affair, while comparation can be a classical math one...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    This is really a hack made by Netscape to ease the creation of event handlers (that is internally, not from your POV). You see, a function does return undefined by default, if there is no return statement to say otherwise. But, undefined autocasts to false and they wanted to make returning false cancel the event. So, they made an internal strict comparison instead of a regular one, which gives the effect that only a boolean false actually cancels the event, as there is no autocasting taking place.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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