Single line if statement with CoffeeScript

In CoffeeScript you can write if-statements with a single line of code. As so often, there are several ways in CoffeeScript to do it. Let me give you some examples:

Source

if hello
  console.log 'Hello World!'

Option 1

if hello then console.log 'Hello World!'

Option 2

console.log 'Hello World!' if hello

And of course:

console.log('Hello World!') if hello
if hello then console.log('Hello World!')

Ein Gedanke zu „Single line if statement with CoffeeScript“

  1. My favourite is the option 2.. the only problem with the one liners is if you want to add more stuff like logging for example you have to break it in two.. and then most likely it will stay two liners 🙂

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.