Coder Social home page Coder Social logo

ternary operator? about coffeescript HOT 7 CLOSED

jashkenas avatar jashkenas commented on May 18, 2024
ternary operator?

from coffeescript.

Comments (7)

ukslim avatar ukslim commented on May 18, 2024 1

@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.

from coffeescript.

jashkenas avatar jashkenas commented on May 18, 2024

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.

from coffeescript.

jots avatar jots commented on May 18, 2024

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 :-)

from coffeescript.

ukslim avatar ukslim commented on May 18, 2024

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"

from coffeescript.

mcanes avatar mcanes commented on May 18, 2024

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

from coffeescript.

michaelficarra avatar michaelficarra commented on May 18, 2024

@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.

from coffeescript.

mcanes avatar mcanes commented on May 18, 2024

@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");

from coffeescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.