Interval comparison doesn't bomb in JS










0















Why doesn't interval comparison bomb in JavaScript?



if(-1 < x < 1) 
console.log('x: ', x)



Why are we allowed to do this without getting errors?



Also it seems that:




  • -1 < x < 1 is true for x<=-1


  • 1 < x < 1 is true for x<=1


  • -1 < x < -1 is always false


  • -2 < x < 2 is always true

In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?










share|improve this question

















  • 1





    They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

    – Bergi
    Nov 15 '18 at 13:15











  • What you really need is -1 < x && x < 1

    – yunzen
    Nov 15 '18 at 13:24











  • @HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

    – Andrea Bergonzo
    Nov 15 '18 at 13:59











  • I knew this. But it would be interesting to other people who want to know, how it's done right

    – yunzen
    Nov 15 '18 at 14:34















0















Why doesn't interval comparison bomb in JavaScript?



if(-1 < x < 1) 
console.log('x: ', x)



Why are we allowed to do this without getting errors?



Also it seems that:




  • -1 < x < 1 is true for x<=-1


  • 1 < x < 1 is true for x<=1


  • -1 < x < -1 is always false


  • -2 < x < 2 is always true

In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?










share|improve this question

















  • 1





    They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

    – Bergi
    Nov 15 '18 at 13:15











  • What you really need is -1 < x && x < 1

    – yunzen
    Nov 15 '18 at 13:24











  • @HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

    – Andrea Bergonzo
    Nov 15 '18 at 13:59











  • I knew this. But it would be interesting to other people who want to know, how it's done right

    – yunzen
    Nov 15 '18 at 14:34













0












0








0








Why doesn't interval comparison bomb in JavaScript?



if(-1 < x < 1) 
console.log('x: ', x)



Why are we allowed to do this without getting errors?



Also it seems that:




  • -1 < x < 1 is true for x<=-1


  • 1 < x < 1 is true for x<=1


  • -1 < x < -1 is always false


  • -2 < x < 2 is always true

In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?










share|improve this question














Why doesn't interval comparison bomb in JavaScript?



if(-1 < x < 1) 
console.log('x: ', x)



Why are we allowed to do this without getting errors?



Also it seems that:




  • -1 < x < 1 is true for x<=-1


  • 1 < x < 1 is true for x<=1


  • -1 < x < -1 is always false


  • -2 < x < 2 is always true

In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?







javascript boolean intervals






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 13:12









Andrea BergonzoAndrea Bergonzo

5171618




5171618







  • 1





    They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

    – Bergi
    Nov 15 '18 at 13:15











  • What you really need is -1 < x && x < 1

    – yunzen
    Nov 15 '18 at 13:24











  • @HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

    – Andrea Bergonzo
    Nov 15 '18 at 13:59











  • I knew this. But it would be interesting to other people who want to know, how it's done right

    – yunzen
    Nov 15 '18 at 14:34












  • 1





    They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

    – Bergi
    Nov 15 '18 at 13:15











  • What you really need is -1 < x && x < 1

    – yunzen
    Nov 15 '18 at 13:24











  • @HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

    – Andrea Bergonzo
    Nov 15 '18 at 13:59











  • I knew this. But it would be interesting to other people who want to know, how it's done right

    – yunzen
    Nov 15 '18 at 14:34







1




1





They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

– Bergi
Nov 15 '18 at 13:15





They are evaluated as (-1 < x) < 1, which is equivalent to (-1 < x) != true or !(-1 < x)

– Bergi
Nov 15 '18 at 13:15













What you really need is -1 < x && x < 1

– yunzen
Nov 15 '18 at 13:24





What you really need is -1 < x && x < 1

– yunzen
Nov 15 '18 at 13:24













@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

– Andrea Bergonzo
Nov 15 '18 at 13:59





@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.

– Andrea Bergonzo
Nov 15 '18 at 13:59













I knew this. But it would be interesting to other people who want to know, how it's done right

– yunzen
Nov 15 '18 at 14:34





I knew this. But it would be interesting to other people who want to know, how it's done right

– yunzen
Nov 15 '18 at 14:34












3 Answers
3






active

oldest

votes


















3














Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1 part. So:



  • When -1 < x is false, the second part is 0 < 1 which is true.


  • When -1 < x is true, the second part is 1 < 1 which is false.


This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.




-1 < x < -1 is always false
-2 < x < 2 is always true


In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?




Using x = -1 and x = 1:



  • If x = -1, then -1 < x is false, so the rest is 0 < -1, which is false.

  • If x = 1, then -1 < 1 is true, so the rest is 1 < -1 which is false.

  • If x = -1, then -2 < -1 is true, so the rest is 1 < -2, which is false.

  • If x = 1, then -2 < 1 is true, so the rest is 1 < -2 which is false.





share|improve this answer
































    0














    I think that this happens because javascript implicitly considers true to have the value 1 and false to have the value 0.



    When you do -1 < x < 1, what you're actually doing is (-1 < x) < 1, or true < 1 if x = 0 which is false.



    However, if x=-2, (-1 < x < -1) will return true as false < 1 is true. Hope this helps.



    You can read more about this here:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators






    share|improve this answer
































      -1














      The reason is that JS interprets your expression.



      if((-1 < x) < 1) 
      console.log('x: ', x)



      Trying using braces...






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320295%2finterval-comparison-doesnt-bomb-in-js%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1 part. So:



        • When -1 < x is false, the second part is 0 < 1 which is true.


        • When -1 < x is true, the second part is 1 < 1 which is false.


        This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.




        -1 < x < -1 is always false
        -2 < x < 2 is always true


        In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?




        Using x = -1 and x = 1:



        • If x = -1, then -1 < x is false, so the rest is 0 < -1, which is false.

        • If x = 1, then -1 < 1 is true, so the rest is 1 < -1 which is false.

        • If x = -1, then -2 < -1 is true, so the rest is 1 < -2, which is false.

        • If x = 1, then -2 < 1 is true, so the rest is 1 < -2 which is false.





        share|improve this answer





























          3














          Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1 part. So:



          • When -1 < x is false, the second part is 0 < 1 which is true.


          • When -1 < x is true, the second part is 1 < 1 which is false.


          This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.




          -1 < x < -1 is always false
          -2 < x < 2 is always true


          In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?




          Using x = -1 and x = 1:



          • If x = -1, then -1 < x is false, so the rest is 0 < -1, which is false.

          • If x = 1, then -1 < 1 is true, so the rest is 1 < -1 which is false.

          • If x = -1, then -2 < -1 is true, so the rest is 1 < -2, which is false.

          • If x = 1, then -2 < 1 is true, so the rest is 1 < -2 which is false.





          share|improve this answer



























            3












            3








            3







            Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1 part. So:



            • When -1 < x is false, the second part is 0 < 1 which is true.


            • When -1 < x is true, the second part is 1 < 1 which is false.


            This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.




            -1 < x < -1 is always false
            -2 < x < 2 is always true


            In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?




            Using x = -1 and x = 1:



            • If x = -1, then -1 < x is false, so the rest is 0 < -1, which is false.

            • If x = 1, then -1 < 1 is true, so the rest is 1 < -1 which is false.

            • If x = -1, then -2 < -1 is true, so the rest is 1 < -2, which is false.

            • If x = 1, then -2 < 1 is true, so the rest is 1 < -2 which is false.





            share|improve this answer















            Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1 part. So:



            • When -1 < x is false, the second part is 0 < 1 which is true.


            • When -1 < x is true, the second part is 1 < 1 which is false.


            This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.




            -1 < x < -1 is always false
            -2 < x < 2 is always true


            In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?




            Using x = -1 and x = 1:



            • If x = -1, then -1 < x is false, so the rest is 0 < -1, which is false.

            • If x = 1, then -1 < 1 is true, so the rest is 1 < -1 which is false.

            • If x = -1, then -2 < -1 is true, so the rest is 1 < -2, which is false.

            • If x = 1, then -2 < 1 is true, so the rest is 1 < -2 which is false.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 13:20

























            answered Nov 15 '18 at 13:15









            T.J. CrowderT.J. Crowder

            694k12212381330




            694k12212381330























                0














                I think that this happens because javascript implicitly considers true to have the value 1 and false to have the value 0.



                When you do -1 < x < 1, what you're actually doing is (-1 < x) < 1, or true < 1 if x = 0 which is false.



                However, if x=-2, (-1 < x < -1) will return true as false < 1 is true. Hope this helps.



                You can read more about this here:
                https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators






                share|improve this answer





























                  0














                  I think that this happens because javascript implicitly considers true to have the value 1 and false to have the value 0.



                  When you do -1 < x < 1, what you're actually doing is (-1 < x) < 1, or true < 1 if x = 0 which is false.



                  However, if x=-2, (-1 < x < -1) will return true as false < 1 is true. Hope this helps.



                  You can read more about this here:
                  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators






                  share|improve this answer



























                    0












                    0








                    0







                    I think that this happens because javascript implicitly considers true to have the value 1 and false to have the value 0.



                    When you do -1 < x < 1, what you're actually doing is (-1 < x) < 1, or true < 1 if x = 0 which is false.



                    However, if x=-2, (-1 < x < -1) will return true as false < 1 is true. Hope this helps.



                    You can read more about this here:
                    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators






                    share|improve this answer















                    I think that this happens because javascript implicitly considers true to have the value 1 and false to have the value 0.



                    When you do -1 < x < 1, what you're actually doing is (-1 < x) < 1, or true < 1 if x = 0 which is false.



                    However, if x=-2, (-1 < x < -1) will return true as false < 1 is true. Hope this helps.



                    You can read more about this here:
                    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 13:25

























                    answered Nov 15 '18 at 13:20









                    theapologisttheapologist

                    576215




                    576215





















                        -1














                        The reason is that JS interprets your expression.



                        if((-1 < x) < 1) 
                        console.log('x: ', x)



                        Trying using braces...






                        share|improve this answer



























                          -1














                          The reason is that JS interprets your expression.



                          if((-1 < x) < 1) 
                          console.log('x: ', x)



                          Trying using braces...






                          share|improve this answer

























                            -1












                            -1








                            -1







                            The reason is that JS interprets your expression.



                            if((-1 < x) < 1) 
                            console.log('x: ', x)



                            Trying using braces...






                            share|improve this answer













                            The reason is that JS interprets your expression.



                            if((-1 < x) < 1) 
                            console.log('x: ', x)



                            Trying using braces...







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 13:20









                            Ram VenkatRam Venkat

                            787




                            787



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320295%2finterval-comparison-doesnt-bomb-in-js%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                Node.js Script on GitHub Pages or Amazon S3

                                Museum of Modern and Contemporary Art of Trento and Rovereto