Is it possible to check for null inline in javascript?

前端 未结 7 2304
孤街浪徒
孤街浪徒 2020-12-15 18:27

I have a function which parses the address components of the Google Maps API JSON and then returns the city / locality / route name.

The getAddre

7条回答
  •  盖世英雄少女心
    2020-12-15 19:06

    What you want is a null coalescent operator. Javascript doesn't have one. Most of the time peoples use the logical OR || for this purpose but it doesn't work on property access.

    There's proposal for adding null coalescing to the language, but it's nowhere near: https://github.com/tc39/proposal-nullish-coalescing
    https://tc39.github.io/proposal-nullish-coalescing/

    If you really, really, absolutly want to use it you can use this Babel plugin: https://www.npmjs.com/package/babel-plugin-transform-optional-chaining
    But I would strongly suggest you don't: this may never make it to the language and you would have unvalid code in your codebase.

提交回复
热议问题