Giter VIP home page Giter VIP logo

Comments (3)

hyrious avatar hyrious commented on June 20, 2024

a == null is effectively equal to a === null || a === undefined, as the spec of ECMAScript said. So it is a totally correct transform.

$ node -p 'a=0;b=1;a??b'
0

$ node -p 'a=0;b=1;a!=null?a:b'
0

from esbuild.

magic-akari avatar magic-akari commented on June 20, 2024

The only difference is document.all.
If you are using Babel, you can achieve code as concise as esbuild's by set https://babeljs.io/docs/assumptions#nodocumentall.

Apart from document.all, they are equivalent.

It appears that esbuild has adopted this strategy by default.

from esbuild.

evanw avatar evanw commented on June 20, 2024

Yes, the document.all edge case is only the reason why some tools have different output than esbuild here. Specifically document.all is the only value in all of JavaScript for which x != null is false but x !== null && x !== undefined is true.

But it's such an obscure edge case that I have decided I don't care about it. FWIW terser, another popular JavaScript minifier, has also decided that they don't care about document.all either: terser/terser#408.

It's ancient web history and doesn't belong in modern JavaScript code. It's not worth bloating everyone's code by transforming ?? into this double-comparison just to handle the document.all value that no one should be using in the first place.

I'm closing this issue as I won't be changing anything here.

Since result of 0 ?? 1 is 0, this may lead to wrong result, is there any settings that can make it right?

Note that 0 ?? 1 and 0 != null ? 0 : 1 are both 0. So esbuild is correct.

from esbuild.

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.