Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. OK, Javascript gurus, why is "if" not evaluating as true or false ?

OK, Javascript gurus, why is "if" not evaluating as true or false ?

Scheduled Pinned Locked Moved JavaScript
javascriptcomdata-structuresfunctionaltutorial
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    For example:

                // Wow, more stupid Javascript.  We can't just do this:
                // return ($(e.target).parents('.jqx-tree').length > 0)
    
                // We actually have to do this.  WTF?
                if ($(e.target).parents('.jqx-tree').length > 0) {
                    return false;
                }
                return true;
    

    WTF is the reason I can't just do a return on > comparison??? Marc

    Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

    Richard DeemingR F 2 Replies Last reply
    0
    • M Marc Clifton

      For example:

                  // Wow, more stupid Javascript.  We can't just do this:
                  // return ($(e.target).parents('.jqx-tree').length > 0)
      
                  // We actually have to do this.  WTF?
                  if ($(e.target).parents('.jqx-tree').length > 0) {
                      return false;
                  }
                  return true;
      

      WTF is the reason I can't just do a return on > comparison??? Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      I can't reproduce the problem. Given:

      var result = $(" ... ").length > 0;
      alert(typeof result);
      alert(result);

      the code displays "boolean" and "true". What output do you get if you try:

      var result = ($(e.target).parents('.jqx-tree').length > 0);
      alert(typeof result);
      alert(result);
      return result;

      As a last resort, there's always the "!!" trick to convert to a boolean value:

      return !!($(e.target).parents('.jqx-tree').length > 0);


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M A 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        I can't reproduce the problem. Given:

        var result = $(" ... ").length > 0;
        alert(typeof result);
        alert(result);

        the code displays "boolean" and "true". What output do you get if you try:

        var result = ($(e.target).parents('.jqx-tree').length > 0);
        alert(typeof result);
        alert(result);
        return result;

        As a last resort, there's always the "!!" trick to convert to a boolean value:

        return !!($(e.target).parents('.jqx-tree').length > 0);


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        Yeah, it looked like a boolean to me too! !! didn't work, but SO provided what I think is a better solution anyways:

            function disableBrowserContextMenu() {
                $(document).on('contextmenu', function (e) {
                    if ($(e.target).parents('.jqx-tree').length > 0) {
                        e.preventDefault()
                    }
                });
            }
        

        Note the e.preventDefault(). Somehow, the explicit return true/false must have been doing this behind the scenes, I'm wildly guessing. Marc

        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

        Richard DeemingR 1 Reply Last reply
        0
        • M Marc Clifton

          Yeah, it looked like a boolean to me too! !! didn't work, but SO provided what I think is a better solution anyways:

              function disableBrowserContextMenu() {
                  $(document).on('contextmenu', function (e) {
                      if ($(e.target).parents('.jqx-tree').length > 0) {
                          e.preventDefault()
                      }
                  });
              }
          

          Note the e.preventDefault(). Somehow, the explicit return true/false must have been doing this behind the scenes, I'm wildly guessing. Marc

          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          In jQuery event handlers, return false; should be directly equivalent to e.preventDefault(); e.stopPropagation();, and usually works to cancel an event. Having said that, e.preventDefault() is the preferred way to stop the event. The difference between ‘return false;’ and ‘e.preventDefault();’[^] jQuery Events: Stop (Mis)Using Return False[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            In jQuery event handlers, return false; should be directly equivalent to e.preventDefault(); e.stopPropagation();, and usually works to cancel an event. Having said that, e.preventDefault() is the preferred way to stop the event. The difference between ‘return false;’ and ‘e.preventDefault();’[^] jQuery Events: Stop (Mis)Using Return False[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #5

            Richard Deeming wrote:

            In jQuery event handlers,

            Heh, this is what happens when a noob (which I still consider myself) starts doing Javascript. Good to know, and thank you for the great info! Marc

            Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              I can't reproduce the problem. Given:

              var result = $(" ... ").length > 0;
              alert(typeof result);
              alert(result);

              the code displays "boolean" and "true". What output do you get if you try:

              var result = ($(e.target).parents('.jqx-tree').length > 0);
              alert(typeof result);
              alert(result);
              return result;

              As a last resort, there's always the "!!" trick to convert to a boolean value:

              return !!($(e.target).parents('.jqx-tree').length > 0);


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              A Offline
              A Offline
              Afzaal Ahmad Zeeshan
              wrote on last edited by
              #6

              I didn't know that !! operator. Thanks. ;-) Removed irrelevant content

              The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

              1 Reply Last reply
              0
              • M Marc Clifton

                For example:

                            // Wow, more stupid Javascript.  We can't just do this:
                            // return ($(e.target).parents('.jqx-tree').length > 0)
                
                            // We actually have to do this.  WTF?
                            if ($(e.target).parents('.jqx-tree').length > 0) {
                                return false;
                            }
                            return true;
                

                WTF is the reason I can't just do a return on > comparison??? Marc

                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                F Offline
                F Offline
                F ES Sitecore
                wrote on last edited by
                #7

                To take a stab at the original question, jquery functions always return collections which is why a) can chain things b) never have to check for nulls. If $(e.target) returned a single thing and parents('.jqx-tree') returned a single thing then your code would make sense and work. But you actually have a foreach loop acting on a foreach loop...the first $() returns a collection (even if that collection only has one item) and the .parents works on each item in that collection and returns its own collection which your .length acts on. Now imaging $(..) returns 3 items (a, b and c) and the .parents of each of those items returns 1,0 and 5 items. a.parents.length = 1 b.parents.length = 0 c.parents.length = 5 now your code

                if ($(e.target).parents('.jqx-tree').length > 0)

                makes less sense, which "length" are you evaluating? This idea about returning collections is probably what is stopping your code from working how you would expect. Maybe :) someone might know better.

                Richard DeemingR 1 Reply Last reply
                0
                • F F ES Sitecore

                  To take a stab at the original question, jquery functions always return collections which is why a) can chain things b) never have to check for nulls. If $(e.target) returned a single thing and parents('.jqx-tree') returned a single thing then your code would make sense and work. But you actually have a foreach loop acting on a foreach loop...the first $() returns a collection (even if that collection only has one item) and the .parents works on each item in that collection and returns its own collection which your .length acts on. Now imaging $(..) returns 3 items (a, b and c) and the .parents of each of those items returns 1,0 and 5 items. a.parents.length = 1 b.parents.length = 0 c.parents.length = 5 now your code

                  if ($(e.target).parents('.jqx-tree').length > 0)

                  makes less sense, which "length" are you evaluating? This idea about returning collections is probably what is stopping your code from working how you would expect. Maybe :) someone might know better.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  But e.target will be a single DOM element, so $(e.target) will be a jQuery wrapper containing a single element. And even if it contained three elements, the .parents(...) call would return a jQuery wrapper containing the matching parents of all three elements. So in your example, the .length property would return 6.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  F 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    But e.target will be a single DOM element, so $(e.target) will be a jQuery wrapper containing a single element. And even if it contained three elements, the .parents(...) call would return a jQuery wrapper containing the matching parents of all three elements. So in your example, the .length property would return 6.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    F Offline
                    F Offline
                    F ES Sitecore
                    wrote on last edited by
                    #9

                    Richard Deeming wrote:

                    But e.target will be a single DOM element, so $(e.target) will be a jQuery wrapper containing a single element.

                    It will be a collection that contains a single element. new string[]{"hello"} is not interchangeable with "hello" Like I said, I don't know js well enough to know if this is the reason that the original code isn't working as intended, I'm just saying it might be a possible reason.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • F F ES Sitecore

                      Richard Deeming wrote:

                      But e.target will be a single DOM element, so $(e.target) will be a jQuery wrapper containing a single element.

                      It will be a collection that contains a single element. new string[]{"hello"} is not interchangeable with "hello" Like I said, I don't know js well enough to know if this is the reason that the original code isn't working as intended, I'm just saying it might be a possible reason.

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      All jQuery wrapper objects are collections, whether they contain multiple elements, a single element, or no elements.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      F 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        All jQuery wrapper objects are collections, whether they contain multiple elements, a single element, or no elements.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        F Offline
                        F Offline
                        F ES Sitecore
                        wrote on last edited by
                        #11

                        Yes, that's what I'm saying :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups