9 JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to _never_ omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:
23 There are, however, some who prefer to only use braces when there is more than one statement to be executed.
27 This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.
33 Examples of **incorrect** code for the default `"all"` option:
38 /*eslint curly: "error"*/
52 Examples of **correct** code for the default `"all"` option:
57 /*eslint curly: "error"*/
78 By default, this rule warns whenever `if`, `else`, `for`, `while`, or `do` are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.
80 Examples of **incorrect** code for the `"multi"` option:
85 /*eslint curly: ["error", "multi"]*/
100 for (var i=0; i < items.length; i++) {
107 Examples of **correct** code for the `"multi"` option:
112 /*eslint curly: ["error", "multi"]*/
128 Alternatively, you can relax the rule to allow brace-less single-line `if`, `else if`, `else`, `for`, `while`, or `do`, while still enforcing the use of curly braces for other instances.
130 Examples of **incorrect** code for the `"multi-line"` option:
135 /*eslint curly: ["error", "multi-line"]*/
149 Examples of **correct** code for the `"multi-line"` option:
154 /*eslint curly: ["error", "multi-line"]*/
156 if (foo) foo++; else doSomething();
184 You can use another configuration that forces brace-less `if`, `else if`, `else`, `for`, `while`, or `do` if their body contains only one single-line statement. And forces braces in all other cases.
186 Examples of **incorrect** code for the `"multi-or-nest"` option:
191 /*eslint curly: ["error", "multi-or-nest"]*/
213 for (var i = 0; foo; i++) {
220 Examples of **correct** code for the `"multi-or-nest"` option:
225 /*eslint curly: ["error", "multi-or-nest"]*/
247 for (var i = 0; foo; i++)
253 For single-line statements preceded by a comment, braces are allowed but not required.
255 Examples of additional **correct** code for the `"multi-or-nest"` option:
260 /*eslint curly: ["error", "multi-or-nest"]*/
276 When using any of the `multi*` options, you can add an option to enforce all bodies of a `if`,
277 `else if` and `else` chain to be with or without braces.
279 Examples of **incorrect** code for the `"multi", "consistent"` options:
284 /*eslint curly: ["error", "multi", "consistent"]*/
314 Examples of **correct** code for the `"multi", "consistent"` options:
319 /*eslint curly: ["error", "multi", "consistent"]*/
349 ## When Not To Use It
351 If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule.