Giter Site home page Giter Site logo

Comments (12)

scottgonzalez avatar scottgonzalez commented on June 11, 2024

I'd really prefer the clarity of 2 over the compressibility of 1.

from contribute.jquery.org.

mgol avatar mgol commented on June 11, 2024

Options x !== x and isNaN(x) are different since isNaN('a') === true etc. I'd prefer the first one since it's not broken as isNaN. We can even implement it as jQuery.isNaN for clarity.

EDIT: @gibson042 has updated his post so I updated mine as well. ;)

from contribute.jquery.org.

gibson042 avatar gibson042 commented on June 11, 2024

Right @mzgol; I updated to the full form for an apples-to-apples comparison.

from contribute.jquery.org.

Krinkle avatar Krinkle commented on June 11, 2024

Agree with @scottgonzalez.

Also, isNaN is not broken. It just casts it to a number first:

Number('a')
// returns: NaN

+'a'
// returns: NaN

+'a' === +'a' // -> NaN === NaN
// returns: false

isNaN('a')
// returns: true

So you shouldn't call isNaN on values that aren't numbers in general, except when ...

  • There is a typeof check first (e.g. when one really needs a strict answer to whether it is NaN)
  • You don't need the typeof check (e.g. when one can reasonably assume it to be a number, like from parseFloat, or if you only care about it not being a number).
    Literally speaking both strings and NaN are "not a number", in the case of old IE length issue, the condition only needed was if ( !isNaN( len ) { (both the typeof check and number casting would be unneeded)

from contribute.jquery.org.

mgol avatar mgol commented on June 11, 2024

Agree with @scottgonzalez https://github.com/scottgonzalez.
Also, isNaN is not broken. It is just casts it to a number first:

That's exactly why it's broken. i'd understand if it just returned true on
non-numbers but having isNaN('a') and isNaN([2, 3]) true and isNaN(''),
isNaN([2]) and isNaN([]) false doesn't make sense from a semantic point of
view.

  • There is a typeof check first (e.g. when one really needs a strict
    answer to whether it is NaN)

I.e. when we make sure we're outside of isNaN insanity region first. In
many cases using x !== x eliminates the need for a typeof check.

Literally speaking both strings and NaN are "not a number",

An empty string is "not a number" and yet isNaN('') is false.

from contribute.jquery.org.

rwaldron avatar rwaldron commented on June 11, 2024

In browsers that support it, we should just use Number.isNaN(n)

Edit: Chrome's Number.isNaN() is wrong

An empty string is "not a number" and yet isNaN('') is true.

I think you meant false

from contribute.jquery.org.

rwaldron avatar rwaldron commented on June 11, 2024

http://wiki.ecmascript.org/doku.php?id=harmony:number.isnan

from contribute.jquery.org.

mgol avatar mgol commented on June 11, 2024

@rwaldron What's wrong about Chrome's Number.isNaN? :/

(and I meant false, obviously, thx for pointing out my mistake; post corrected)

from contribute.jquery.org.

mikesherov avatar mikesherov commented on June 11, 2024

Since option 1 and option 2 are 2 different things, I'm not sure why this needs to be in the style guide. If it's unclear, either we should introduce a function that provides clarity, or add a clarifying comment.

from contribute.jquery.org.

arschmitz avatar arschmitz commented on June 11, 2024

I'm not in favor of option 1 i think this is confusing and since this is used very very little i cant see how the size difference is significant enough to warrant how confusing it is. I don't think the average dev knows that NaN is the only thing thats not equal to it self .isNaN is much clearer

from contribute.jquery.org.

dmethvin avatar dmethvin commented on June 11, 2024

The strict equality check feels like a trivia challenge since most people could live their life as JS programmers not knowing NaN is unequal to itself. Since we know it's a number at this point, it seems like isNaN() is the most straightforward way even if it's a few more bytes. I don't think we need a style guide issue for this though, the system worked if this was discussed in code review.

from contribute.jquery.org.

scottgonzalez avatar scottgonzalez commented on June 11, 2024

At this point it's safe to say that this won't go into our style guide. I would also not expect option 1 to be added in any more places than the one place it currently exists in Sizzle.

from contribute.jquery.org.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.