+ 1
var a = 1=='1'?return 1:return 0;
Why does this code generate syntax error? (It was a SL match)
5 Respuestas
+ 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);
+ 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
+ 1
Daniel Asherov Great, Daniel! I have learnt sonething new from you. Have a nice day!