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
);