2 title: no-use-before-define
7 In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
9 In ES6, block-level bindings (`let` and `const`) introduce a "temporal dead zone" where a `ReferenceError` will be thrown with any attempt to access the variable before its declaration.
13 This rule will warn when it encounters a reference to an identifier that has not yet been declared.
15 Examples of **incorrect** code for this rule:
20 /*eslint no-use-before-define: "error"*/
69 Examples of **correct** code for this rule:
74 /*eslint no-use-before-define: "error"*/
129 "no-use-before-define": ["error", {
133 "allowNamedExports": false
138 * `functions` (`boolean`) -
139 The flag which shows whether or not this rule checks function declarations.
140 If this is `true`, this rule warns every reference to a function before the function declaration.
141 Otherwise, ignores those references.
142 Function declarations are hoisted, so it's safe.
144 * `classes` (`boolean`) -
145 The flag which shows whether or not this rule checks class declarations of upper scopes.
146 If this is `true`, this rule warns every reference to a class before the class declaration.
147 Otherwise, ignores those references if the declaration is in upper function scopes.
148 Class declarations are not hoisted, so it might be danger.
150 * `variables` (`boolean`) -
151 This flag determines whether or not the rule checks variable declarations in upper scopes.
152 If this is `true`, the rule warns every reference to a variable before the variable declaration.
153 Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
155 * `allowNamedExports` (`boolean`) -
156 If this flag is set to `true`, the rule always allows references in `export {};` declarations.
157 These references are safe even if the variables are declared later in the code.
160 This rule accepts `"nofunc"` string as an option.
161 `"nofunc"` is the same as `{ "functions": false, "classes": true, "variables": true, "allowNamedExports": false }`.
165 Examples of **correct** code for the `{ "functions": false }` option:
170 /*eslint no-use-before-define: ["error", { "functions": false }]*/
178 This option allows references to function declarations. For function expressions and arrow functions, please see the [`variables`](#variables) option.
182 Examples of **incorrect** code for the `{ "classes": false }` option:
187 /*eslint no-use-before-define: ["error", { "classes": false }]*/
221 Examples of **correct** code for the `{ "classes": false }` option:
226 /*eslint no-use-before-define: ["error", { "classes": false }]*/
240 Examples of **incorrect** code for the `{ "variables": false }` option:
245 /*eslint no-use-before-define: ["error", { "variables": false }]*/
254 const g = function() {};
281 Examples of **correct** code for the `{ "variables": false }` option:
286 /*eslint no-use-before-define: ["error", { "variables": false }]*/
294 function b() { return f(); }
295 const c = function() { return f(); }
298 const e = function() { return g(); }
299 const g = function() {}
311 ### allowNamedExports
313 Examples of **correct** code for the `{ "allowNamedExports": true }` option:
318 /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/
320 export { a, b, f, C };
333 Examples of **incorrect** code for the `{ "allowNamedExports": true }` option:
338 /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/
346 export function foo() {