Omission of if still works for code block

I would like to be able to omit the if () part of an if statement.

Current:

if (myCondition) {
   // code
   // code
}

Proposed:

myCondition {
   // code
   // code
}
(myCondition) {
   // code
   // code
}

Ternary sort of works for this but it is only one line:

myCondition ? // true code : // false code;

What problem is it solving to omit if?

also, that would conflict with what is currently valid syntax, condition \n { statement; } or condition \n { objectLiteral }, and we don't usually at "no newline" restrictions without a good reason.

I have two use cases multiline for ternary and multiline for && operator.

Essentially, it's ternary but multiline inside of a block that allows for comments.

It removes the use of if and the requirement of parenthesis.

Ternary single line:

condition ? doThing : 0;

The && operator. I frequently use the && operator to check for true and then run code after but the same as above, it doesn't support multiline:

condition && codeToRunIfTrue;

The if statement without if and else:

// the following would be the same 
if (condition) { }
condition {}

While without the while :

while (condition) { 
   // comments supported
   // multiline supported
}

Proposal - Ternary multiline:

condition { 
   // do thing
   // comments supported
   // multiline supported
}

What does this mean? Can you show examples?

My personal opinion is that good code style doesn't abuse value selection operators for flow control (ternary, and &&) and that using the explicit if would be better.

Why is it a problem worth solving and worth adding complexity to the language and parser/tooling ecosystem that you have to type the four characters of i, f, (, and )?

1 Like

I mean that that's existing syntax (\n represents a newline).

Why improve something? Why save time?
This is a site for suggesting things that people and users of JavaScript think they would need and believe that these changes might improve their life. I already mentioned that it does. The effort I put into posting this question with examples means that it matters to me. If you post a feature request on one of my projects I'm not going to question your veracity. If you posted it t's shows enough that it matters to you to take into consideration.

To sum up, I post suggestions because I believe it will improve my quality of life (in regards to my coding experience); the same as anyone who goes about inventing anything. And it may improve the quality of life of other JavaScript users.

I would argue that it doesn't. Making syntax more ambiguous (i.e. having many ways to express the same thing) is a bad thing: it makes code harder to read/understand, the language harder to learn, and slows down development.

It is not clear what kind of time you think would be saved (parse time, development time, learnig time, debugging time, typing time, …).

Ambiguous. What is this:

yield {
  x: y()
}

either current semantics:

yield /*object*/ ({ x: y() })

or proposed omitted if:

if (yield /*undefined*/) {
  x: // label
    y()
}

It supports multiline, but with , instead of ; and without declaration and operators like if, while:

true && (console.log(4),
a = 7,
alert(4))