Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript do expression: if statement without else

I have a question about the proposed JavaScript do expression construct. There are many examples that show how it can be used to return a value from a conditional statement that has both if and else. Also valid for if with else if and else.

What about a conditional that has just an if clause but no else if or else? Is this valid usage of the do expression?

My use case is for conditionally displaying content in a React component. I would like to write JSX code like so, but I am not sure if it is valid:

export default function myComponent(props) {
  return (
    <div>
      {do {
        if (true) {
          <p>If statement is true</p>
        }
      }}
      <p>I am always rendered</p>
    </div>
  );
}

I also asked the question in this gist.

like image 528
ecbrodie Avatar asked Aug 14 '17 04:08

ecbrodie


People also ask

Can I have an if statement without else JavaScript?

An if statement looks at any and every thing in the parentheses and if true, executes block of code that follows. If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed.

Is it necessary to add else after ELSE IF?

So I hope not tl;dr the first questions answer is no, a else can follow after an if but it does not need to, the other way is completely prohibited (else without if), makes sense as this would mean do it in all cases, where you would not need a condition, so it is expected to be an unwanted error.

CAN YOU DO IF statements in JavaScript?

Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run. There are multiple different types of conditionals in JavaScript including: “If” statements: where if a condition is true it is used to specify execution for a block of code.


3 Answers

Not sure about do, also as mentioned by @Felix Kling

do expression is a stage 1 proposal, not part of ES2016 (ES7).


You can write it like this using ternary operator, and return null if condition fails:

export default function myComponent(props) {
    return (
        <div>
            {
                true?
                    <p>If statement is true</p>
                :null
            }
            <p>I am always rendered</p>
        </div>
    );
}

Or Use && Operator:

export default function myComponent(props) {
    return (
        <div>
            {
                true && <p>If statement is true</p>
            }
            <p>I am always rendered</p>
        </div>
    );
}

Check the DOC for more details about Conditional Rendering.

like image 93
Mayank Shukla Avatar answered Oct 30 '22 15:10

Mayank Shukla


Why not simply do this ? Using the Ternary operator

export default function myComponent(props) {
  return (
    <div>
      { true ? <p>If statement is true</p> : null }
      <p>I am always rendered</p>
    </div>
  );
}
like image 44
Kush Patel Avatar answered Oct 30 '22 15:10

Kush Patel


Here is what Babel does:

Input:

<p>
  {do {
    if (true) {
      <a/>
    }
  }}
</p>

Output:

React.createElement(
  "p",
  null,
  true ? React.createElement("a", null) : void 0
);

This makes sense to me. The return value in the implied else would be undefined.

like image 22
Joe Frambach Avatar answered Oct 30 '22 15:10

Joe Frambach