Skip to Content
Maintainable JavaScript
book

Maintainable JavaScript

by Nicholas C. Zakas
May 2012
Intermediate to advanced
242 pages
5h 19m
English
O'Reilly Media, Inc.
Content preview from Maintainable JavaScript

Chapter 3. Statements and Expressions

Statements such as if and for can be used in two ways in JavaScript, with curly braces for multiple contained lines or without curly braces for one contained line. For example:

// Bad, though technically valid JavaScript
if(condition)
    doSomething();

// Bad, though technically valid JavaScript
if(condition) doSomething();

// Good
if (condition) {
    doSomething();
}

// Bad, though technically valid JavaScript
if (condition) { doSomething(); }

The first two forms, which use an if statement without braces, are explicitly disallowed in Crockford’s Code Conventions, the jQuery Core Style Guide, the SproutCore Style Guide, and the Dojo Style Guide. The omission of braces also generates warnings by default in both JSLint and JSHint.

An overwhelming majority of JavaScript developers are in agreement that block statements should always use braces and always occupy multiple lines instead of one. This is because of the confusion created when braces aren’t included. Consider the following:

if (condition)
    doSomething();
    doSomethingElse();

It’s difficult to tell the author’s intent in this code. There’s clearly an error here, but it’s impossible to know whether the error is an indentation error (the last line should not be indented) or braces are missing because both line 2 and line 3 need to be executed inside the if statement. Adding braces makes the error easier to find. Here are two other examples with errors:

if (condition) { doSomething(); } doSomethingElse(); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Rediscovering JavaScript

Rediscovering JavaScript

Venkat Subramaniam
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9781449328092Purchase bookErrata Page