Is it possible to use 'break' statement in ternary operator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to use 'break' statement in ternary operator?

I tried to write a loop in javascript without condition and increment. var i = 0; for (; ; ) { document.write(i); (i < 10)? (i++): (break); } But i got a error says: "SyntaxError: expected expression, got keyword 'break' " is there any way i can use break in ternary operator

18th Sep 2017, 12:06 PM
Md. Ashifur Rahaman
Md. Ashifur Rahaman - avatar
4 Answers
+ 4
The ternary conditional operator is an operator that combines multiple expressions into a larger expression. break is a statement and not an expression, so it can't be used inside a ternary conditional expression. -from Google. for (let i = 0; i < 10; i++) { document.write(i); } Same thing you wanted to do,here's no reason in ternary operator.
18th Sep 2017, 12:19 PM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 7
Why don't you go for while, if and else statement instead- while (true) { if (i<10) document.write(i); else break; } There is no way you can go for break in ternary operator.
18th Sep 2017, 12:41 PM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
0
@Rose I know the other ways. But I wanted to know is there any way to use break in ternary. But thanx for answering.
18th Sep 2017, 5:21 PM
Md. Ashifur Rahaman
Md. Ashifur Rahaman - avatar
- 3
wait up break continue this 2 things are purely for loops.. only used and only can be used in loops.. u cant use them outside loops.. it will give u error
18th Sep 2017, 4:47 PM
sayan chandra
sayan chandra - avatar