15 JavaScript allows you to place unnecessary spaces before or after a semicolon.
17 Disallowing or enforcing space around a semicolon can improve the readability of your program.
22 var c = "d";var e = "f";
27 This rule aims to enforce spacing around a semicolon. This rule prevents the use of spaces before a semicolon in expressions.
29 This rule doesn't check spacing in the following cases:
31 * The spacing after the semicolon if it is the first token in the line.
33 * 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`.
35 * The spacing around the semicolon in a for loop with an empty condition (`for(;;)`).
39 The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`.
40 If `before` is `true`, space is enforced before semicolons and if it's `false`, space is disallowed before semicolons.
41 If `after` is `true`, space is enforced after semicolons and if it's `false`, space is disallowed after semicolons.
42 The `after` option will be only applied if a semicolon is not at the end of line.
44 The default is `{"before": false, "after": true}`.
47 "semi-spacing": ["error", {"before": false, "after": true}]
50 ### `{"before": false, "after": true}`
52 This is the default option. It enforces spacing after semicolons and disallows spacing before semicolons.
54 Examples of **incorrect** code for this rule:
59 /*eslint semi-spacing: "error"*/
63 throw new Error("error") ;
65 for (i = 0 ; i < 10 ; i++) {}
66 for (i = 0;i < 10;i++) {}
71 Examples of **correct** code for this rule:
76 /*eslint semi-spacing: "error"*/
80 throw new Error("error");
82 for (i = 0; i < 10; i++) {}
90 ### `{"before": true, "after": false}`
92 This option enforces spacing before semicolons and disallows spacing after semicolons.
94 Examples of **incorrect** code for this rule with the `{"before": true, "after": false}` option:
99 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
103 throw new Error("error");
105 for (i = 0;i < 10;i++) {}
106 for (i = 0; i < 10; i++) {}
111 Examples of **correct** code for this rule with the `{"before": true, "after": false}` option:
116 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
120 throw new Error("error") ;
122 for (i = 0 ;i < 10 ;i++) {}
127 ## When Not To Use It
129 You can turn this rule off if you are not concerned with the consistency of spacing before or after semicolons.