Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • 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
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. The JavaScript type coercion algorithm

The JavaScript type coercion algorithm

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
12 Posts 9 Posters 4 Views
  • 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.
  • cm0002@lemmy.worldC [email protected]

    That's it. That's the meme.

    lefrog@discuss.tchncs.deL This user is from outside of this forum
    lefrog@discuss.tchncs.deL This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #2

    TL;DR do not use ==

    V 1 Reply Last reply
    12
    • cm0002@lemmy.worldC [email protected]

      That's it. That's the meme.

      S This user is from outside of this forum
      S This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #3

      It's so crazy to me that they convert from string to int and not the other way round.

      V 1 Reply Last reply
      7
      • lefrog@discuss.tchncs.deL [email protected]

        TL;DR do not use ==

        V This user is from outside of this forum
        V This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #4

        Common practice since forever

        D 1 Reply Last reply
        4
        • S [email protected]

          It's so crazy to me that they convert from string to int and not the other way round.

          V This user is from outside of this forum
          V This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #5

          "1e2" == 100 I guess is the reason? I mean if there were some "reasons" to begin with 😮‍💨

          S 1 Reply Last reply
          5
          • V [email protected]

            "1e2" == 100 I guess is the reason? I mean if there were some "reasons" to begin with 😮‍💨

            S This user is from outside of this forum
            S This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #6

            There surely are strings that can be converted into numbers (even something as simple as "1"). But in general, when languages do implicit conversions, they do it towards the more general type.

            For example, if I do 1.1 == 1 in pretty much any language that has separate float and int types, the integer gets casted into a float (from more specific to more general type) and the comparison returns false. It would be utterly ridiculous if the language auto-casts the float to int and then returns true.

            JS does just that. Instead of casting the more general number into a string and comparing that, it goes the other way round.

            Every number has an equivalent string representation, not every string has an equivalent number representation.

            tetragrade@leminal.spaceT 1 Reply Last reply
            1
            • cm0002@lemmy.worldC [email protected]

              That's it. That's the meme.

              N This user is from outside of this forum
              N This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #7

              if you hate JS and think it's dumb, but still have to use it, look into Eloquent Javascript (free) https://eloquentjavascript.net/ and the mozilla docs https://developer.mozilla.org/en-US/docs/Web/JavaScript.

              Eloquent JS is written by someone who also hates JS, but understands it very well.

              I used them while I worked on the Microtonal Music Grid (still in progress)

              1 Reply Last reply
              1
              • cm0002@lemmy.worldC [email protected]

                That's it. That's the meme.

                veganpizza69@lemmy.vgV This user is from outside of this forum
                veganpizza69@lemmy.vgV This user is from outside of this forum
                [email protected]
                wrote last edited by
                #8

                Just use ===

                1 Reply Last reply
                1
                • V [email protected]

                  Common practice since forever

                  D This user is from outside of this forum
                  D This user is from outside of this forum
                  [email protected]
                  wrote last edited by
                  #9

                  most linters since like jshint as well catch it

                  1 Reply Last reply
                  0
                  • S [email protected]

                    There surely are strings that can be converted into numbers (even something as simple as "1"). But in general, when languages do implicit conversions, they do it towards the more general type.

                    For example, if I do 1.1 == 1 in pretty much any language that has separate float and int types, the integer gets casted into a float (from more specific to more general type) and the comparison returns false. It would be utterly ridiculous if the language auto-casts the float to int and then returns true.

                    JS does just that. Instead of casting the more general number into a string and comparing that, it goes the other way round.

                    Every number has an equivalent string representation, not every string has an equivalent number representation.

                    tetragrade@leminal.spaceT This user is from outside of this forum
                    tetragrade@leminal.spaceT This user is from outside of this forum
                    [email protected]
                    wrote last edited by
                    #10

                    Not quite. As the previous commenter said, every string has at least one string representation (i.e. 100 -> "100", "1e2"). So there's no sensible way to write a pure function handling that, you're just cooked no matter what you do.

                    S 1 Reply Last reply
                    0
                    • tetragrade@leminal.spaceT [email protected]

                      Not quite. As the previous commenter said, every string has at least one string representation (i.e. 100 -> "100", "1e2"). So there's no sensible way to write a pure function handling that, you're just cooked no matter what you do.

                      S This user is from outside of this forum
                      S This user is from outside of this forum
                      [email protected]
                      wrote last edited by
                      #11

                      Other languages handle that easily:

                      Implicit cast from number to string, explicit parsing in the other direction.

                      For comparisons as number, parse the string to a number and compare two numbers.

                      The main point of the implicit cast from number to string is to concattenate number to string, e.g. print("testValue: " + testValue).

                      Another option (e.g. taken by Python) is to acknowledge that there's no pure 1:n mapping in any direction between string and number, so any conversion between those two needs to be done explicitly. That's probably the most correct implementation, but it makes string concattenation annoying. But then again, f-strings and similar techniques make that problem pretty much obsolete.

                      tetragrade@leminal.spaceT 1 Reply Last reply
                      1
                      • S [email protected]

                        Other languages handle that easily:

                        Implicit cast from number to string, explicit parsing in the other direction.

                        For comparisons as number, parse the string to a number and compare two numbers.

                        The main point of the implicit cast from number to string is to concattenate number to string, e.g. print("testValue: " + testValue).

                        Another option (e.g. taken by Python) is to acknowledge that there's no pure 1:n mapping in any direction between string and number, so any conversion between those two needs to be done explicitly. That's probably the most correct implementation, but it makes string concattenation annoying. But then again, f-strings and similar techniques make that problem pretty much obsolete.

                        tetragrade@leminal.spaceT This user is from outside of this forum
                        tetragrade@leminal.spaceT This user is from outside of this forum
                        [email protected]
                        wrote last edited by
                        #12

                        Yeah I mean it's definitely possible to write a mostly sensible string-number equality function that only breaks in edge-cases, but at this point it's all kinda vibes-based mush, and the real question is like... Why would you want to do that? What are you really trying to achieve?

                        The most likely case is that it's a novice that doesn't understand what they're doing and the Python setup you describe does a better job at setting up guardrails.

                        I don't really see the connection to concatenation, that's kind of its own thing.

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


                        • Login

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