简体   繁体   中英

What is this “?.” feature called, in JavaScript?

Just realized it exists, thanks to VSCode enforcing it...

  • I don't know what it is called;
  • It's not TypeScript.

Usage

let regex = /hot/g;
let phrase = "It's hot, outside.";

// you don't need to worry about null
let matches = phrase.match(regex)?.length;

That feature is called optional chaining . This calls the method when the receiver is neither undefined nor null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM