Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ternary operator? #46

Closed
jots opened this issue Jan 6, 2010 · 7 comments
Closed

ternary operator? #46

jots opened this issue Jan 6, 2010 · 7 comments
Labels

Comments

@jots
Copy link

jots commented Jan 6, 2010

Are there plans to allow the use of the ternary operator in coffee-script?
I'm converting some code that uses it a lot.
Also, will "===" be allowed in the future? right now i convert them to "==" which coffee script converts back to "===".

@jashkenas
Copy link
Owner

I use the ternary operator a lot too, and CoffeeScript uses it whenever possible (because it's already an expression). There's no special syntax for it in CoffeeScript, just this:

lunch: if wednesday then spaghetti else lasagna

Compiles into:

var lunch;
lunch = wednesday ? spaghetti : lasagna;

I realize its slightly longer, but less cryptic, I hope. If you want to break it up, this will compile in to the exact same JavaScript ternary:

lunch: if wednesday
  spaghetti
else
  lasagna

As to the triple equals, there is not going to ever be a == in CoffeeScript. You can use == for triple equals (like what it means in most languages), or you can use is, and isnt.

@jots
Copy link
Author

jots commented Jan 6, 2010

Thanks Jeremy, that works fine, i didn't know about that syntax. however if you ever decide to allow a straight ternary syntax, you'll have my vote :-)

@ukslim
Copy link

ukslim commented Jul 28, 2011

It's a real gotcha in my opinion because a C/Java/JS style ternary doesn't cause the compiler to complain, but gives unexpected results.

alert 5 > 4 ? "bigger" : "not bigger" # alerts "true"

@mcanes
Copy link

mcanes commented Jul 28, 2011

alert(5 > 4 ? "bigger" : "not bigger") # alerts "bigger"

@ukslim
Copy link

ukslim commented Jul 28, 2011

@mcanes Where are you trying this? Both my local CoffeeScript install, and the 'Try CoffeeScript' page, alert 'true' with that code. Both with and without your parentheses.

@michaelficarra
Copy link
Collaborator

@ukslim: this issue is more than a year and a half old and long settled. If you have a question, please open a new issue, ask it on IRC, or ask it in the google group.

@mcanes
Copy link

mcanes commented Jul 28, 2011

@ukslim On the 'Try CoffeeScript' page using chrome and Firefox

This is the coffee script code

alert( if 5 > 4 then "bigger" else "not bigger")

and the generated js

alert(5 > 4 ? "bigger" : "not bigger");

int3 pushed a commit to int3/coffee-script that referenced this issue Feb 14, 2013
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants