50
github
7y

Basically,
1+infinity = 1
But
infinity + 1 = NaN
My entire maths fundamental shuttered by the beautiful JS language....

Comments
  • 1
    But NaN is a number. JavaScript...
  • 12
    If I'm correct, parse functions don't do any math. So it stops at the +. The first two are words, so it comes back as NaN, and the last one is a 1.

    At least that's my best guess.

    If a language allowed you to parse "0/0" as an int it would make injections much easier.
  • 1
    @AlgoRythm haha.. but I think all validations always works in server side nowadays and JS hacks won't pass through...
  • 2
    @AlgoRythm You are kinda correct. It simply extracts digits until it finds something that is not digit.

    parseInt("test1") =>NaN
    parseInt("1test") => 1
    "1+infinity" => "1+infinity"
    parseInt("1+4") => 1
  • 1
  • 2
    @AlgoRythm how did I missed it!!! 😱😱😱
  • 1
    @paziek yup.. I also assume it's the same logic.. but still it should return NaN since it's not a number
  • 1
    @filthyranter lol.. nan is not a number.. !javascript
  • 0
  • 5
    The unfounded JavaScript hate is real.

    It's not like Integer.Parse("1+4") gives you 5 either.

    If you try to use a saw to tighten a screw, don't blame the tool.
  • 2
    @github unfortunately that isn't the case, The number of sites/apps which only do front-end validation for critical data is > 0

    I know of a few, 1 is an ecommerce platform where you can even specify the price you want to pay through the frontend due to some senior retards being retarded and managment refusing to give the few non retards working there access to fix it because "noone else knows about it so its not a problem")
  • 0
    @github It shouldn't, because parseInt is not made for casting, but for extracting and converting integer from provided value. It will return NaN only if value fails to meet requirements described in here: https://ecma-international.org/ecma...
    or here: https://w3schools.com/jsref/... MDN: https://developer.mozilla.org/en-US...

    You can't just assume what function does simply by looking at its name and taking wild guess.
  • 1
    @hawkes 😂😂😂 the unfounded js hate..
    This was just a joke..
    I am an avid fan of js though.. currently planning to work on making a parser during coming weekends..
  • 1
    My advice is to cast all variables to what you expect them to be, then you won't have to worry about all those implicit behind-the-scenes things going on when using weak typed languages.

    parseInt(Number("1")+Number("Infinity")) => NaN
    because
    Number("1")+Number("Infinity") => Infinity

    1-"Infinity" => -Infinity
    1+"Infinity" => "1Infinity"
    1+Number("Infinity") => Infinity

    You get numerical operator in first, because strings don't have "-" operator. You get string operator in second, because if there is string on either side of operator AND said operator is supported by string, then that will be always used. Third example has numeric values on both sides, so numeric operator is used.
  • 2
    @paziek you took it too seriously dude.. thanks for sharing the links and your elaborated explanation..
    I have worked in production codes in js with proper code reviews from senior devs too.. and knows good amount of stuffs in js when to do what..
    I just posted it as joke.. and such snippets should never be used in actual codes..
  • 0
    So now we are reposting the reposts? even looks like taken from that exact repost gif... 😥
  • 1
    @JoshBent nah.. I took it from my Mac.. using my phone and Firefox browser console..
  • 0
    @github This exact same thing has been posted atleast 20 times, either reposted in worse quality to fool the system, taken straight from reddit, added some "😂😂😂😂😂" etc.

    There has been discussions, rants about it being reposts, philosophers trying to tryhard intepret "infinity" and more, its getting to a point where its posted atleast bi-weekly.. 😥
  • 2
    @JoshBent shit... That much frequent.. we should have moderator system.. so, if u were it, u could have deleted my rant instantly.. leading me to another rant ranting for deletion of my rant in devrant..
  • 0
    @github Don't take it that rough, you just happen to have used the same case many repost "bots" on here abused.. 😅
  • 1
    @JoshBent you mean to say devrant have repost bots?? Bot accounts?
  • 0
    That's exactly the behaviour I would expect to see, knowing how the parseInt function works.

    Documentation. Read it.
  • 0
    > Nan === Nan
    false

    This sealed the deal for me hahaha
  • 1
    @Tale-Of-X9 why would one ever expect NaN to be equal to NaN? Would you expect the following to be true?

    Math.sqrt(-1) === (0 / 0); // ???

    Math.sqrt(-1); // NaN
    0 / 0; // NaN
  • 1
    @github I put quotation marks around bots to prevent that question, but what I meant was that certain people are straight up acting like bots by just going everyday to the one specific subreddit and post that content to devrant or by just searching for older posts, saving those in lower quality and reposting them. But I wouldn't go as far as saying there isn't any actual bots, because creating one for devrant is one of the easiest tasks.
  • 1
    That's why I love JS, you dunno what's gonna happen xD
Add Comment