1 # Enforce spacing before and after semicolons (semi-spacing)
3 JavaScript allows you to place unnecessary spaces before or after a semicolon.
5 Disallowing or enforcing space around a semicolon can improve the readability of your program.
10 var c = "d";var e = "f";
15 This rule aims to enforce spacing around a semicolon. This rule prevents the use of spaces before a semicolon in expressions.
17 This rule doesn't check spacing in the following cases:
19 * The spacing after the semicolon if it is the first token in the line.
21 * The spacing before the semicolon if it is after an opening parenthesis (`(` or `{`), or the spacing after the semicolon if it is before a closing parenthesis (`)` or `}`). That spacing is checked by `space-in-parens` or `block-spacing`.
23 * The spacing around the semicolon in a for loop with an empty condition (`for(;;)`).
27 The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`.
28 If `before` is `true`, space is enforced before semicolons and if it's `false`, space is disallowed before semicolons.
29 If `after` is `true`, space is enforced after semicolons and if it's `false`, space is disallowed after semicolons.
30 The `after` option will be only applied if a semicolon is not at the end of line.
32 The default is `{"before": false, "after": true}`.
35 "semi-spacing": ["error", {"before": false, "after": true}]
38 ### `{"before": false, "after": true}`
40 This is the default option. It enforces spacing after semicolons and disallows spacing before semicolons.
42 Examples of **incorrect** code for this rule:
45 /*eslint semi-spacing: "error"*/
49 throw new Error("error") ;
51 for (i = 0 ; i < 10 ; i++) {}
52 for (i = 0;i < 10;i++) {}
55 Examples of **correct** code for this rule:
58 /*eslint semi-spacing: "error"*/
62 throw new Error("error");
64 for (i = 0; i < 10; i++) {}
70 ### `{"before": true, "after": false}`
72 This option enforces spacing before semicolons and disallows spacing after semicolons.
74 Examples of **incorrect** code for this rule with the `{"before": true, "after": false}` option:
77 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
81 throw new Error("error");
83 for (i = 0;i < 10;i++) {}
84 for (i = 0; i < 10; i++) {}
87 Examples of **correct** code for this rule with the `{"before": true, "after": false}` option:
90 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
94 throw new Error("error") ;
96 for (i = 0 ;i < 10 ;i++) {}
101 You can turn this rule off if you are not concerned with the consistency of spacing before or after semicolons.
106 * [no-extra-semi](no-extra-semi.md)
107 * [comma-spacing](comma-spacing.md)
108 * [block-spacing](block-spacing.md)
109 * [space-in-parens](space-in-parens.md)