1 # Disallow assignment to native objects or read-only global variables (no-global-assign)
3 JavaScript environments contain a number of built-in global variables, such as `window` in browsers and `process` in Node.js. In almost all cases, you don't want to assign a value to these global variables as doing so could result in losing access to important functionality. For example, you probably don't want to do this in browser code:
9 While examples such as `window` are obvious, there are often hundreds of built-in global objects provided by JavaScript environments. It can be hard to know if you're assigning to a global variable or not.
13 This rule disallows modifications to read-only global variables.
15 ESLint has the capability to configure global variables as read-only.
17 * [Specifying Environments](../user-guide/configuring#specifying-environments)
18 * [Specifying Globals](../user-guide/configuring#specifying-globals)
20 Examples of **incorrect** code for this rule:
23 /*eslint no-global-assign: "error"*/
30 /*eslint no-global-assign: "error"*/
31 /*eslint-env browser*/
39 /*eslint no-global-assign: "error"*/
45 Examples of **correct** code for this rule:
48 /*eslint no-global-assign: "error"*/
56 /*eslint no-global-assign: "error"*/
57 /*eslint-env browser*/
59 onload = function() {}
63 /*eslint no-global-assign: "error"*/
71 This rule accepts an `exceptions` option, which can be used to specify a list of builtins for which reassignments will be allowed:
76 "no-global-assign": ["error", {"exceptions": ["Object"]}]
83 If you are trying to override one of the native objects.
87 * [no-extend-native](no-extend-native.md)
88 * [no-redeclare](no-redeclare.md)
89 * [no-shadow](no-shadow.md)