简体   繁体   中英

Javascript two-lined if/else statement. What is this called?

There's a one-lined if/else statement (ternary operations), as seen here .

What about the two-lined ones?

eg

if (true) console.log("True");
else console.log("False");

edit: The 'normal' / standard if/else statements I see look like this:

if (true) {
   console.log("True");
} else {
   console.log("False");
}

So the difference: No curly brackets.

The conditional operator, or ternary operator, is called just that - the ternary operator or conditional operator. It is not called a "single-lined if-else statement" because, well, there's no if-else in it. (It might be similar to the effect of an if-else as a single expression, but that doesn't make it one)

The code in your question is just a plain if-else statement, since it uses if-else - no matter how many lines it has.

Your example is just a standart if-else. The absence of brackets is possible because you have only 1 statment inside the conditions. This can be applied anywhere where you would use brackets to have multiple statments. For example:

while(true) console.log("looping")

It's just differences in coding style; see @VLAZ's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM