4

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;
6
  • 1
    It's the "optional chaining operator", as per caniuse.com/#search=%3F. Commented Aug 29, 2020 at 1:48
  • 1
    As people answered it's called optional chaining, however it's also called elvis operator sometimes. Commented Aug 29, 2020 at 1:51
  • 1
    @YairCohen Why is it called the "Elvis Operator"? Commented Aug 29, 2020 at 1:51
  • 1
    "The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley with his quiff." - Wikipedia Commented Aug 29, 2020 at 1:52
  • 1
    Optional chaining. I believe it's an ES6 feature. Commented Aug 29, 2020 at 1:58

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Commonly combined with the nullish coalescing operator (??)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.