+ 1

var a = 1=='1'?return 1:return 0;

Why does this code generate syntax error? (It was a SL match)

23rd Feb 2020, 3:46 PM
Prof. Dr. Zoltán Vass
5 Respostas
+ 6
That's certainly wrong syntax. If you are sure you have seen similar question in challenge report it. return can't appear outside a function. the ternary operator *returns* 2nd or 3rd operand depending upon whether 1st operand evaluates to true or false. If one needs to do this explicitly using return statement it'll be necessary to wrap the statement within a function. var a = (()=>{return 1=='1'? 0: 1})(); alert(a);
23rd Feb 2020, 4:05 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
If inside a function, you can also use the ternary if like this to return a value without wrapping it in a variable first: return 1 == "1"? 1 : 0 if the condition resolves to true, 1 is returned, else 0 is returned
23rd Feb 2020, 6:44 PM
Daniel Asherov
+ 1
Daniel Asherov Great, Daniel! I have learnt sonething new from you. Have a nice day!
24th Feb 2020, 5:13 AM
Prof. Dr. Zoltán Vass