JavaScript single line ‘if’ statement – best syntax, this alternative?

I’ve seen the short-circuiting behaviour of the && operator used to achieve this, although people who are not accustomed to this may find it hard to read or even call it an anti-pattern:

lemons && document.write("foo gave me a bar");  

Personally, I’ll often use single-line if without brackets, like this:

if (lemons) document.write("foo gave me a bar");

If I need to add more statements in, I’ll put the statements on the next line and add bracket

Leave a Comment