Https://www.freecodecamp.org/learn/front-end-libraries/react/use--for-a-more-concise-conditional

Tell us what’s happening:
Describe your issue in detail here.
I don’t understand how this line of code checks a condition without any if/else syntax :
{this.state.display &&

Displayed!

}

  **Your code so far**

class MyComponent extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    display: true
  }
  this.toggleDisplay = this.toggleDisplay.bind(this);
}
toggleDisplay() {
  this.setState(state => ({
    display: !state.display
  }));
}
render() {
  
  return (
     <div>
       <button onClick={this.toggleDisplay}>Toggle Display</button>
       {this.state.display &&  <h1>Displayed!</h1>}
     </div>
  );
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Use && for a More Concise Conditional

Link to the challenge:

Hello there,

That is just what the && (and) operator does:

expr1 && expr2

" If expr1 can be converted to true , returns expr2 ; else, returns expr1 ."

I suggest you run this code in your favourite environment to experiment with the output:

const a = "I am truthy" && "So am I";
const b = null && "Do I show?"
const c = b || a; // Just for fun

console.log(`${a}\n${b}\n${c}`);

Hope this helps

PS there are many different circuit operators

its a one line syntax, or even an inline syntax to do something, if something is true. It works similarly to the ternary operators-

condition ? is true : else  // if (condition) { ...is true }; else ...

but there is no else transition, there is only an if

condition && is true  // if (condition) { ...is true }

Thank You for your help

Thank you for your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.