KittiesAreCute!
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
OsrsNeedsF2P@lemmy.ml to Programmer Humor@lemmy.ml · 2 years ago

Yup, Javascript can go F@#! itself

lemmy.ml

message-square
11
link
fedilink
13

Yup, Javascript can go F@#! itself

lemmy.ml

OsrsNeedsF2P@lemmy.ml to Programmer Humor@lemmy.ml · 2 years ago
message-square
11
link
fedilink
alert-triangle
You must log in or # to comment.
  • Xylight (Photon dev)@lemmy.xylight.dev
    link
    fedilink
    arrow-up
    2
    ·
    2 years ago

    POV: you don’t understand type coercion

  • Julian@lemm.ee
    link
    fedilink
    arrow-up
    1
    ·
    2 years ago

    Ok some of these I understand but what the fuck. Why.

    • RagingToad@feddit.nl
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 years ago

      I’m not sure if you really want to know, but:

      greater than, smaller than, will cast the type so it will be 0>0 which is false, ofcourse. 0>=0 is true.

      Now == will first compare types, they are different types so it’s false.

      Also I’m a JavaScript Dev and if I ever see someone I work with use these kind of hacks I’m never working together with them again unless they apologize a lot and wash their dirty typing hands with… acid? :-)

      edit: as several people already pointed out, my answer is not accurate. The real solution was mentioned by mycus

  • TwilightKiddy@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    2 years ago

    This one is one of my favourite JS quirks:

    JS quirk

    • LeFrog@discuss.tchncs.de
      link
      fedilink
      arrow-up
      0
      ·
      2 years ago

      Wait wtf is happening there?

      • usernamesAreTricky@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        parseInt is meant for strings so it converts the number there into a string. Once the numbers get small enough it starts representing it with scientific notation. So 0.0000001 converts into "1e-7" where it then starts to ignore the e-7 part because that’s not a valid int, so it is left with 1

        https://javascript.plainenglish.io/why-parseint-0-0000001-0-8fe1aec15d8b

  • Björn Tantau@feddit.de
    link
    fedilink
    arrow-up
    0
    ·
    2 years ago

    Can someone explain this? I mean, the last result. Usually I can at least understand Javascript’s or PHP’s quirks. But this time I’m stumped.

    • mycus@kbin.social
      link
      fedilink
      arrow-up
      0
      ·
      2 years ago

      JS null and undefined shenanigans


      basically:

      1. bigger an lesser comparison types convert null to zero, so is zero bigger or lesser than zero? no
      2. == is fucky and to it null only equals undefined and undefined only equals null (and themselves), so no
      3. is zero bigger than or equal to zero? yeah
      • Björn Tantau@feddit.de
        link
        fedilink
        arrow-up
        0
        ·
        2 years ago

        Ugh, thanks, of course. Stupid brain.

        • mycus@kbin.social
          link
          fedilink
          arrow-up
          0
          ·
          2 years ago

          I’m starting to think JS maintainers have a thing against mathematicians

          • Quik2007@kbin.social
            link
            fedilink
            arrow-up
            1
            ·
            2 years ago

            more likely against humans

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 118 users / day
  • 753 users / week
  • 2.18K users / month
  • 8.01K users / 6 months
  • 2 local subscribers
  • 38.8K subscribers
  • 1.93K Posts
  • 32.5K Comments
  • Modlog
  • mods:
  • AgreeableLandscape@lemmy.ml
  • cat_programmer@lemmy.ml
  • BE: 0.19.12
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org