Settings

Theme

WTF does this code do?

benvan.co.uk

10 points by benvan 12 years ago · 8 comments

Reader

sigvef 12 years ago

This is why you should always use semicolons in JavaScript. Omitting semicolons here turns this:

  var angry = say('Garrrr!')
  
  // what should I say?
  (amIHappy ? happy : angry)()
into this:

  var angry = say('Garrrr!')(amIHappy ? happy : angry)()
zequel 12 years ago

Most web browsers (if you're running this code in a web browser), will do ASI (automatic semicolon insertion).

http://jsfiddle.net/CxUXh/ works fine in Chrome, changing the console.log to an alert.

Not that I'm advocating omitting semicolons!

  • benvanOP 12 years ago

    Interesting - your jsfiddle exhibits broken behaviour (alerting 'garrr') on my browser - chrome 33.0.1750.152. I believe I'm fairly up to date.

    • zequel 12 years ago

      You're correct, I thought that was expected behavior, my mistake. TBH though, I'm surprised ASI didn't fix the problem, I'll have to look at the rules for insertion again.

dsschnau 12 years ago

Does it return that because `say` isn't defined inside its own function definition, so it evaluates as false?

TophWells 12 years ago

It doesn't seem to do anything.

rainmaking 12 years ago

Hahaha, nice!

I'm getting a linter.

dismal2 12 years ago

this is idiotic

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection