1 # Disallow Early Use (no-use-before-define)
3 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.
5 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.
9 This rule will warn when it encounters a reference to an identifier that has not yet been declared.
11 Examples of **incorrect** code for this rule:
14 /*eslint no-use-before-define: "error"*/
58 Examples of **correct** code for this rule:
61 /*eslint no-use-before-define: "error"*/
111 "no-use-before-define": ["error", { "functions": true, "classes": true, "variables": true }]
115 * `functions` (`boolean`) -
116 The flag which shows whether or not this rule checks function declarations.
117 If this is `true`, this rule warns every reference to a function before the function declaration.
118 Otherwise, ignores those references.
119 Function declarations are hoisted, so it's safe.
121 * `classes` (`boolean`) -
122 The flag which shows whether or not this rule checks class declarations of upper scopes.
123 If this is `true`, this rule warns every reference to a class before the class declaration.
124 Otherwise, ignores those references if the declaration is in upper function scopes.
125 Class declarations are not hoisted, so it might be danger.
127 * `variables` (`boolean`) -
128 This flag determines whether or not the rule checks variable declarations in upper scopes.
129 If this is `true`, the rule warns every reference to a variable before the variable declaration.
130 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.
133 This rule accepts `"nofunc"` string as an option.
134 `"nofunc"` is the same as `{ "functions": false, "classes": true, "variables": true }`.
138 Examples of **correct** code for the `{ "functions": false }` option:
141 /*eslint no-use-before-define: ["error", { "functions": false }]*/
147 This option allows references to function declarations. For function expressions and arrow functions, please see the [`variables`](#variables) option.
151 Examples of **incorrect** code for the `{ "classes": false }` option:
154 /*eslint no-use-before-define: ["error", { "classes": false }]*/
186 Examples of **correct** code for the `{ "classes": false }` option:
189 /*eslint no-use-before-define: ["error", { "classes": false }]*/
201 Examples of **incorrect** code for the `{ "variables": false }` option:
204 /*eslint no-use-before-define: ["error", { "variables": false }]*/
213 const g = function() {};
238 Examples of **correct** code for the `{ "variables": false }` option:
241 /*eslint no-use-before-define: ["error", { "variables": false }]*/
249 function b() { return f(); }
250 const c = function() { return f(); }
253 const e = function() { return g(); }
254 const g = function() {}