I need to set value to a
that depends on a condition.
What is the shortest way to do this with CoffeeScript?
E.g. this is how I'd do it in JavaScript:
a = true ? 5 : 10 # => a = 5 a = false ? 5 : 10 # => a = 10
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
Single-line Comments. Whenever we want to comment a single line in CoffeeScript, we just need to place a hash tag before it as shown below. Every single line that follows a hash tag (#) is considered as a comment by the CoffeeScript compiler and it compiles the rest of the code in the given file except the comments.
One crucial difference between the two languages is that TypeScript is the superset of JavaScript while CoffeeScript is a language which is an enhanced version of JavaScript. Not just these two languages but there are other languages such as Dart, Kotlin, etc. which can be compiled into JavaScript.
Since everything is an expression, and thus results in a value, you can just use if/else
.
a = if true then 5 else 10 a = if false then 5 else 10
You can see more about expression examples here.
a = if true then 5 else 10 a = if false then 5 else 10
See documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With