Jump to content

Conditional Operator ? less keystrokes : back to basics


esmith

Recommended Posts

Dan used a conditional operator to replace an if/then/else statement when defining a variable in this post.

 

Can the conditional operator be used to return values instead of assigning them? I can't seem to validate this code:

(Field("Indicia") == "1st Class") ? return Resource("Resource1") : return NullResource();

but this works just fine:

if (Field("Indicia") == "1st Class") return Resource("Resource1");
else return NullResource();

 

Can I get an advanced lesson on when I can use the conditional operator versus typing the entire if/else statement?

 

P.S. Does anyone else have problems trying to get the CODE tags to work correctly on the first try?!

Link to comment
Share on other sites

Can the conditional operator be used to return values instead of assigning them? I can't seem to validate this code:

(Field("Indicia") == "1st Class") ? return Resource("Resource1") : return NullResource();

but this works just fine:

if (Field("Indicia") == "1st Class") return Resource("Resource1");
else return NullResource();

 

Can I get an advanced lesson on when I can use the conditional operator versus typing the entire if/else statement?

You're close, but remember that the return value of the conditional operator [noparse](?:)[/noparse] is a value, so you don't want your "return" statement inside it. I think this is what you want:

return (Field("Indicia") == "1st Class") ? Resource("Resource1") : NullResource();

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Conditional_Operator

P.S. Does anyone else have problems trying to get the CODE tags to work correctly on the first try?!

What do you mean by "the first try?" You can always use the Preview Post button to make sure the post is right, or edit it after you post.

Link to comment
Share on other sites

Thanks for the clarification.

 

As for the P.S. -- I'll have to get a video capture of my screen the next time I try to use the CODE tags. It seems like the first time I use them and click preview, I get everything up to the first tag and the rest gets chopped off. I then have to copy and paste the original text in pieces until I can get it all to display as intended. Maybe it's a Safari issue? Or is it just an esmith issue? ;)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...