skip to Main Content

I have been encountering with this conditional operator,

phone={this.props.projectDetails?.agency?.phone ?? this.props.projectDetails.phone }

I wanted to use Phone number that is given in agency?.phone but at some point we didn’t had phone number in agency so we were using projectDetail’s phone so overcome this issue.

3

Answers


  1. Nullish coalescing operator means If is null or undefined you will render projectDetails.phone prop

    Login or Signup to reply.
  2. In case if we don’t have any else condition , use Nullish coalescing operator (??)

    this.props.projectDetails?.agency?.phone (if it has value) ?? (then show) this.props.projectDetails.phone

    .. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing

    Login or Signup to reply.
  3. it’s Nullish coalescing operator and in your case exactly equal to this :

    phone={this.props.projectDetails?.agency?.phone ? this.props.projectDetails.agency.phone : this.props.projectDetails.phone }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search