I adopted Douglas Crockford's JavaScript code convention some time ago, but at yesterday's @media Ajax he demonstrated, in a beautifully simple way, why it's really important.

MY EVENTAttend ffconf.org 2018

The UK's best JS and web development conference. 8 amazing speakers, workshops, socials — find out more & get tickets today.

The following two examples look the same, but they're not:

// the right way
return {
  'ok': false

// the wrong way
  'ok': false

The reason why the second way is wrong, is because JavaScript's semicolon injection in the second version is actually processed as this:

return; // which returns undefined

// block level of code that does nothing
  'ok': false

; // this last semicolon is executed as a dummy line

The return code doesn't fail or throw any errors, but as soon as you try to access your result, your code will break since the returned value is undefined rather than an object.

Want more?

Posts, web development learnings & insights, exclusive workshop and training discounts and more, direct to your inbox.

I won't send you any spam, and you can unsubscribe at any time. Powered by ConvertKit