SDF Chatter
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
TechCodex@programming.dev to Programmer Humor@lemmy.mlEnglish · 2 years ago

Javascript can create atomic bombs

programming.dev

message-square
63
fedilink
607

Javascript can create atomic bombs

programming.dev

TechCodex@programming.dev to Programmer Humor@lemmy.mlEnglish · 2 years ago
message-square
63
fedilink
  • adrian783@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    2 years ago

    2 equal signs will coerce the second operand into the type of first operand then do a comparison of it can. so 1 == “1” is true. this leads to strange bugs.

    3 equal signs do not do implicit type conversion, cuts down on weird bugs. 1===“1” is false.

    edit: it appears to be more complicated than that for double equals and the position of operands don’t matter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality

    • TonyTonyChopper@mander.xyz
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      wow that seems super useful, thanks

    • Rikudou_Sage@lemmings.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      Doesn’t it widen the types regardless of position? Meaning 1 == “1” will be compared as strings, not numbers.

      • adrian783@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality

        mdn goes into it more and it’s way more involved than I thought, looks like order of operand doesn’t matter. see the number to string section

        • Rikudou_Sage@lemmings.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          2 years ago

          It seems it is that way, which is weird. You should always convert to the widest type, meaning string for comparing numbers and strings. I just checked that 1 == "01" is true, which means that “01” gets cast to an integer. And according to the document it might be that for example 1 == "a" would basically be interpreted as 1 === NaN which is false.

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.

  • 95 users / day
  • 1.13K users / week
  • 2.56K users / month
  • 10.2K users / 6 months
  • 437 local subscribers
  • 35.8K subscribers
  • 1.73K Posts
  • 38.3K Comments
  • Modlog
  • mods:
  • AgreeableLandscape@lemmy.ml
  • cat_programmer@lemmy.ml
  • BE: 0.19.8
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org