1 # space-before-keywords: enforce consistent spacing before keywords
3 (removed) This rule was **removed** in ESLint v2.0 and **replaced** by the [keyword-spacing](keyword-spacing.md) rule.
5 (fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixed problems reported by this rule.
7 Keywords are syntax elements of JavaScript, such as `function` and `if`. These identifiers have special meaning to the language and so often appear in a different color in code editors. As an important part of the language, style guides often refer to the spacing that should be used around keywords. For example, you might have a style guide that says keywords should be always be preceded by spaces, which would mean `if-else` statements must look like this:
17 Of course, you could also have a style guide that disallows spaces before keywords.
21 This rule will enforce consistency of spacing before the keywords `if`, `else`, `for`,
22 `while`, `do`, `switch`, `throw`, `try`, `catch`, `finally`, `with`, `break`, `continue`,
23 `return`, `function`, `yield`, `class` and variable declarations (`let`, `const`, `var`)
26 This rule takes one argument: `"always"` or `"never"`. If `"always"` then the keywords
27 must be preceded by at least one space. If `"never"` then no spaces will be allowed before
28 the keywords `else`, `while` (do...while), `finally` and `catch`. The default value is `"always"`.
30 This rule will allow keywords to be preceded by an opening curly brace (`{`). If you wish to alter
31 this behavior, consider using the [block-spacing](block-spacing.md) rule.
33 Examples of **incorrect** code for this rule with the default `"always"` option:
36 /*eslint space-before-keywords: ["error", "always"]*/
43 const foo = 'bar';let baz = 'qux';
45 var foo =function bar () {}
52 Examples of **correct** code for this rule with the default `"always"` option:
55 /*eslint space-before-keywords: ["error", "always"]*/
64 <Foo onClick={function bar() {}} />
66 for (let foo of ['bar', 'baz', 'qux']) {}
69 Examples of **incorrect** code for this rule with the `"never"` option:
72 /*eslint space-before-keywords: ["error", "never"]*/
88 Examples of **correct** code for this rule with the `"never"` option:
91 /*eslint space-before-keywords: ["error", "never"]*/
104 ## When Not To Use It
106 If you do not wish to enforce consistency on keyword spacing.
110 * [space-after-keywords](space-after-keywords.md)
111 * [block-spacing](block-spacing.md)
112 * [space-return-throw-case](space-return-throw-case.md)
113 * [space-unary-ops](space-unary-ops.md)
114 * [space-infix-ops](space-infix-ops.md)