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. Other Discussions
  3. The Weird and The Wonderful
  4. YARTH-JS : Yet Another Reason To Hate JavaScript

YARTH-JS : Yet Another Reason To Hate JavaScript

Scheduled Pinned Locked Moved The Weird and The Wonderful
javascriptcomdata-structureshelpquestion
15 Posts 9 Posters 45 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.
  • raddevusR Offline
    raddevusR Offline
    raddevus
    wrote on last edited by
    #1

    As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

    var extra = null; // feeling like you are initializing a var for later use.

    // ## Now, try either of the following lines
    extra += 'a' + 'b' + 'c';
    extra += "abc";

    // Guess what the value of extra is??? G'head and guess.
    // Yes it is "nullabc" What!?!

    // But try it with a number
    var numberTest = null; // feeling like you are initializing a var for later use.
    numberTest += 55 + 33 + 38;
    //Guess what the value is?
    // Yep, 126

    X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

    Richard DeemingR K M D S 6 Replies Last reply
    0
    • raddevusR raddevus

      As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

      var extra = null; // feeling like you are initializing a var for later use.

      // ## Now, try either of the following lines
      extra += 'a' + 'b' + 'c';
      extra += "abc";

      // Guess what the value of extra is??? G'head and guess.
      // Yes it is "nullabc" What!?!

      // But try it with a number
      var numberTest = null; // feeling like you are initializing a var for later use.
      numberTest += 55 + 33 + 38;
      //Guess what the value is?
      // Yep, 126

      X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

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

      raddevus wrote:

      I guess it pre-decides that it's a string so it figures it should convert the null object to the null string.

      Nope - it "decides" when it executes the addition operator. JavaScript Addition Operator in Details[^] If one operand is a string, the other is converted to a string and the values are concatenated. String(null) === "null" Otherwise, they are converted to numbers and added. Number(null) === 0 And yes, it sucks. Just be grateful it doesn't borrow VB's habit of coercing strings that look like numbers into numbers, adding them together, and then converting the result back to a string. :)


      "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

      raddevusR 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        raddevus wrote:

        I guess it pre-decides that it's a string so it figures it should convert the null object to the null string.

        Nope - it "decides" when it executes the addition operator. JavaScript Addition Operator in Details[^] If one operand is a string, the other is converted to a string and the values are concatenated. String(null) === "null" Otherwise, they are converted to numbers and added. Number(null) === 0 And yes, it sucks. Just be grateful it doesn't borrow VB's habit of coercing strings that look like numbers into numbers, adding them together, and then converting the result back to a string. :)


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

        raddevusR Offline
        raddevusR Offline
        raddevus
        wrote on last edited by
        #3

        Richard Deeming wrote:

        Nope - it "decides" when it executes the addition operator.

        Thanks for the info. :thumbsup: It's a very good point and I should've thought of it myself -- that it occurs when the concatenation is done.

        1 Reply Last reply
        0
        • raddevusR raddevus

          As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

          var extra = null; // feeling like you are initializing a var for later use.

          // ## Now, try either of the following lines
          extra += 'a' + 'b' + 'c';
          extra += "abc";

          // Guess what the value of extra is??? G'head and guess.
          // Yes it is "nullabc" What!?!

          // But try it with a number
          var numberTest = null; // feeling like you are initializing a var for later use.
          numberTest += 55 + 33 + 38;
          //Guess what the value is?
          // Yep, 126

          X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

          K Offline
          K Offline
          kmoorevs
          wrote on last edited by
          #4

          :laugh: Nice example! :thumbsup: I learned something. :)

          "Go forth into the source" - Neal Morse

          1 Reply Last reply
          0
          • raddevusR raddevus

            As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

            var extra = null; // feeling like you are initializing a var for later use.

            // ## Now, try either of the following lines
            extra += 'a' + 'b' + 'c';
            extra += "abc";

            // Guess what the value of extra is??? G'head and guess.
            // Yes it is "nullabc" What!?!

            // But try it with a number
            var numberTest = null; // feeling like you are initializing a var for later use.
            numberTest += 55 + 33 + 38;
            //Guess what the value is?
            // Yep, 126

            X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

            M Offline
            M Offline
            Martin ISDN
            wrote on last edited by
            #5

            if you learn the rules of coercion you may love the language. it's nothing ambiguous. you will become more expressive, but your expressions may look like gibberish to people coming from more strict languages. on the other hand if you too are used to stricter language, didn't if bother you that you are assigning null to extra and then adding characters or integers to it? don't you fell more natural to define extra as "" if you add strings to it or as 0 if you add integers to it?

            raddevusR L 2 Replies Last reply
            0
            • M Martin ISDN

              if you learn the rules of coercion you may love the language. it's nothing ambiguous. you will become more expressive, but your expressions may look like gibberish to people coming from more strict languages. on the other hand if you too are used to stricter language, didn't if bother you that you are assigning null to extra and then adding characters or integers to it? don't you fell more natural to define extra as "" if you add strings to it or as 0 if you add integers to it?

              raddevusR Offline
              raddevusR Offline
              raddevus
              wrote on last edited by
              #6

              sickfile wrote:

              didn't if bother you that you are assigning null to extra and then adding characters or integers to it?

              That's actually a good point and good thinking. I actually do like JavaScript, I just find that I get bit by these things and there are a lot of esoteric things to know about the language that cause my brain to burn more calories than I probably should have to. But that is the natural laziness that all we devs have I suppose. The real thing that bothers me about JavaScript is the fact that there are two values which are so similar but are different:

              undefined
              null

              I could just use one or the other really. But then I become unusre at times and think, "hmm...is JS going to think this is undefined at this point? Is it going to think it is null?" then additionally in the case of a string I have to wonder if it is an empty string too (and there is no String.Empty to compare to so I have to use double-quotes ""). These are the things that are just bothersome. But they are bothersome because I cannot concentrate on JavaScript as much as I'd like too, since I'm an attempting to create solutions in software -- not just understand pedantic syntax. I think that is why at times, I finally just have to rant about JS. :)

              M J 3 Replies Last reply
              0
              • raddevusR raddevus

                sickfile wrote:

                didn't if bother you that you are assigning null to extra and then adding characters or integers to it?

                That's actually a good point and good thinking. I actually do like JavaScript, I just find that I get bit by these things and there are a lot of esoteric things to know about the language that cause my brain to burn more calories than I probably should have to. But that is the natural laziness that all we devs have I suppose. The real thing that bothers me about JavaScript is the fact that there are two values which are so similar but are different:

                undefined
                null

                I could just use one or the other really. But then I become unusre at times and think, "hmm...is JS going to think this is undefined at this point? Is it going to think it is null?" then additionally in the case of a string I have to wonder if it is an empty string too (and there is no String.Empty to compare to so I have to use double-quotes ""). These are the things that are just bothersome. But they are bothersome because I cannot concentrate on JavaScript as much as I'd like too, since I'm an attempting to create solutions in software -- not just understand pedantic syntax. I think that is why at times, I finally just have to rant about JS. :)

                M Offline
                M Offline
                Martin ISDN
                wrote on last edited by
                #7

                that's ok, we all rant here and there. null is an object. it has it's purposes. it just takes time to make a significant difference between null and undefined. not so long ago i have stumbled upon a situation at work where a coworker had decided to write the absence of an object in the database simply as 0, instead of null. all i can tell you is, and i remembered well, that the logic i had to use, because the field was either an object or 0 in it's absence, was like 3 boolean expressions concatenated with || or &&. if the absence of the object was marked with null i would have to use only one boolean expression. even tho if (0) and if (null) yield the same result. there are many articles on the net that explain the differences between: 0, "", undefined, null,... etc. and the subject can be sometimes so confusing that you would have to go through the comments of the article to figure it out and i'm talking about the best articles out there. no good text will only confuse a person.

                1 Reply Last reply
                0
                • raddevusR raddevus

                  sickfile wrote:

                  didn't if bother you that you are assigning null to extra and then adding characters or integers to it?

                  That's actually a good point and good thinking. I actually do like JavaScript, I just find that I get bit by these things and there are a lot of esoteric things to know about the language that cause my brain to burn more calories than I probably should have to. But that is the natural laziness that all we devs have I suppose. The real thing that bothers me about JavaScript is the fact that there are two values which are so similar but are different:

                  undefined
                  null

                  I could just use one or the other really. But then I become unusre at times and think, "hmm...is JS going to think this is undefined at this point? Is it going to think it is null?" then additionally in the case of a string I have to wonder if it is an empty string too (and there is no String.Empty to compare to so I have to use double-quotes ""). These are the things that are just bothersome. But they are bothersome because I cannot concentrate on JavaScript as much as I'd like too, since I'm an attempting to create solutions in software -- not just understand pedantic syntax. I think that is why at times, I finally just have to rant about JS. :)

                  M Offline
                  M Offline
                  Martin ISDN
                  wrote on last edited by
                  #8

                  hey, i remember bookmarking your Practical Electronics For Makers articles just a few days ago. nice work!

                  1 Reply Last reply
                  0
                  • raddevusR raddevus

                    sickfile wrote:

                    didn't if bother you that you are assigning null to extra and then adding characters or integers to it?

                    That's actually a good point and good thinking. I actually do like JavaScript, I just find that I get bit by these things and there are a lot of esoteric things to know about the language that cause my brain to burn more calories than I probably should have to. But that is the natural laziness that all we devs have I suppose. The real thing that bothers me about JavaScript is the fact that there are two values which are so similar but are different:

                    undefined
                    null

                    I could just use one or the other really. But then I become unusre at times and think, "hmm...is JS going to think this is undefined at this point? Is it going to think it is null?" then additionally in the case of a string I have to wonder if it is an empty string too (and there is no String.Empty to compare to so I have to use double-quotes ""). These are the things that are just bothersome. But they are bothersome because I cannot concentrate on JavaScript as much as I'd like too, since I'm an attempting to create solutions in software -- not just understand pedantic syntax. I think that is why at times, I finally just have to rant about JS. :)

                    J Offline
                    J Offline
                    jsc42
                    wrote on last edited by
                    #9

                    raddevus wrote:

                    there are two values which are so similar but are different:

                    undefined
                    null

                    or three. Don't forget

                    void 0

                    1 Reply Last reply
                    0
                    • M Martin ISDN

                      if you learn the rules of coercion you may love the language. it's nothing ambiguous. you will become more expressive, but your expressions may look like gibberish to people coming from more strict languages. on the other hand if you too are used to stricter language, didn't if bother you that you are assigning null to extra and then adding characters or integers to it? don't you fell more natural to define extra as "" if you add strings to it or as 0 if you add integers to it?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      sickfile wrote:

                      you will become more expressive, but your expressions may look like gibberish to people coming from more strict languages.

                      Which makes stuff easy for maintenance, ofcourse.

                      sickfile wrote:

                      on the other hand if you too are used to stricter language, didn't if bother you that you are assigning null to extra and then adding characters or integers to it?

                      No, it would result in a compiler error.

                      sickfile wrote:

                      don't you fell more natural to define extra as "" if you add strings to it or as 0 if you add integers to it?

                      Yes, either and int or string, not a var. We don't DIM.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      1 Reply Last reply
                      0
                      • raddevusR raddevus

                        As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

                        var extra = null; // feeling like you are initializing a var for later use.

                        // ## Now, try either of the following lines
                        extra += 'a' + 'b' + 'c';
                        extra += "abc";

                        // Guess what the value of extra is??? G'head and guess.
                        // Yes it is "nullabc" What!?!

                        // But try it with a number
                        var numberTest = null; // feeling like you are initializing a var for later use.
                        numberTest += 55 + 33 + 38;
                        //Guess what the value is?
                        // Yep, 126

                        X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

                        D Offline
                        D Offline
                        Dominic Burford
                        wrote on last edited by
                        #11

                        Type coercion in JS has long been a known area of difficulty. However, if you learn it's peculiar rules, you may start to appreciate the method to the madness :)

                        "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

                        1 Reply Last reply
                        0
                        • raddevusR raddevus

                          As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

                          var extra = null; // feeling like you are initializing a var for later use.

                          // ## Now, try either of the following lines
                          extra += 'a' + 'b' + 'c';
                          extra += "abc";

                          // Guess what the value of extra is??? G'head and guess.
                          // Yes it is "nullabc" What!?!

                          // But try it with a number
                          var numberTest = null; // feeling like you are initializing a var for later use.
                          numberTest += 55 + 33 + 38;
                          //Guess what the value is?
                          // Yep, 126

                          X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

                          S Offline
                          S Offline
                          Super Lloyd
                          wrote on last edited by
                          #12

                          Good to know! :) The good thing with JavaScript is that there is an endless stream of trick trivia question that can be made with it! :laugh: But it's better left to... others... :O

                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                          raddevusR 1 Reply Last reply
                          0
                          • S Super Lloyd

                            Good to know! :) The good thing with JavaScript is that there is an endless stream of trick trivia question that can be made with it! :laugh: But it's better left to... others... :O

                            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                            raddevusR Offline
                            raddevusR Offline
                            raddevus
                            wrote on last edited by
                            #13

                            Super Lloyd wrote:

                            there is an endless stream of trick trivia question that can be made with it! :laugh:

                            Yeah, I often see those posted all over the place.

                            1 Reply Last reply
                            0
                            • raddevusR raddevus

                              As if you need another reason. :rolleyes: But here is a good one that maybe you've seen before.

                              var extra = null; // feeling like you are initializing a var for later use.

                              // ## Now, try either of the following lines
                              extra += 'a' + 'b' + 'c';
                              extra += "abc";

                              // Guess what the value of extra is??? G'head and guess.
                              // Yes it is "nullabc" What!?!

                              // But try it with a number
                              var numberTest = null; // feeling like you are initializing a var for later use.
                              numberTest += 55 + 33 + 38;
                              //Guess what the value is?
                              // Yep, 126

                              X| Here's a snapshot (via imgur) of the FireFox dev console[^] in case you find it difficult to believe. I've been using JS for quite some time and try to stay up on these types of things but this one bit me today. Really quite annoying. I guess it pre-decides that it's a string so it figures it should convert the null object to the null string. I'm so glad it does that!!! X| X| :mad::mad: Really a PEBKAC error but still quite annoying.

                              M Offline
                              M Offline
                              markrlondon
                              wrote on last edited by
                              #14

                              raddevus wrote:

                              var extra = null; // feeling like you are initializing a var for later use.

                              Isn't this sort of thing why TypeScript was invented? ;-) In TypeScript:

                              var extra:string;

                              raddevusR 1 Reply Last reply
                              0
                              • M markrlondon

                                raddevus wrote:

                                var extra = null; // feeling like you are initializing a var for later use.

                                Isn't this sort of thing why TypeScript was invented? ;-) In TypeScript:

                                var extra:string;

                                raddevusR Offline
                                raddevusR Offline
                                raddevus
                                wrote on last edited by
                                #15

                                markrlondon wrote:

                                Isn't this sort of thing why TypeScript was invented?

                                Yes it is! I will switch to TS one day too. I'm just so lazy I haven't gotten around to it. :laugh:

                                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