I just watched a video of Douglas Crockford’s presentation about his 2009 book JavaScript: The Good Parts.
In the video, he explains that the following block is dangerous because it produces silent errors:
return
{
ok: false
};
And that it should actually be written like this (emphasising that although seemingly identical the behavioural difference is crucial):
return {
ok: false
};
You can see his comments around 32 minutes into the video here: http://www.youtube.com/watch?v=hQVTIJBZook&feature=player_embedded#!&start=1920
I have not heard this before, and was wondering if this rule still applies or if this requirement in syntax has been overcome by JavaScript developments since this statement was made.
I found this very interesting as I have NOT been writing my code this way, and wanted to check that this information was not out of date.
The silent error is that
undefinedis returned!Semicolons are optional in JavaScript, and therefore
is parsed as if it were
JSLint will recognize such patterns and warn about them:
This has been discussed on SO in the “Strangest language feature” question.