admin管理员组文章数量:1405748
Is there a way to continue expression on the next line in JS?
const result = 'one' ? 1 :
'two' ? 2 :
3
turn it into
const result = \
'one' ? 1 :
'two' ? 2 :
3
and turn this
return condition1 &&
condition2 &&
condition3
into
return \
condition1 &&
condition2 &&
condition3
So it would looks better?
It's possible to do it like that but I hope there are better way
return true &&
condition1 &&
condition2 &&
condition3
Is there a way to continue expression on the next line in JS?
const result = 'one' ? 1 :
'two' ? 2 :
3
turn it into
const result = \
'one' ? 1 :
'two' ? 2 :
3
and turn this
return condition1 &&
condition2 &&
condition3
into
return \
condition1 &&
condition2 &&
condition3
So it would looks better?
It's possible to do it like that but I hope there are better way
return true &&
condition1 &&
condition2 &&
condition3
Share
edited Aug 4, 2019 at 0:01
Jack Bashford
44.2k11 gold badges55 silver badges82 bronze badges
asked Aug 3, 2019 at 23:55
Alex CraftAlex Craft
15.5k14 gold badges90 silver badges156 bronze badges
3 Answers
Reset to default 5Your first desired snippet
const result =
'one' ? 1 :
'two' ? 2 :
3
is already allowed, but due to automatic semicolon insertion (ASI), the return
statement must be written as:
return (
condition1 &&
condition2 &&
condition3
)
Yes - just use parentheses for the return
one:
return (
condition1 &&
condition2 &&
condition3
);
If you leave a newline after the return
keyword, it'll return undefined
.
Your ternary operator can be used as-is, but consider also using parentheses to decrease confusion:
const result =
"one" ? 1 : (
"two" ? 2 : 3
);
This is very much about personal preference, here's how I like to write those:
const result =
"one"
? 1
: ("two"
? 2
: 3);
return (
condition1
&& condition2
&& condition3
);
本文标签: if statementJavaScripthow to continue expression on the next lineStack Overflow
版权声明:本文标题:if statement - JavaScript, how to continue expression on the next line - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744922409a2632374.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论