# no-global-this

Rule `sonarjs/no-global-this`. The global "this" object should not be used

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1273 by reach, #574 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-global-this` | yes | suggestion | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2990/) |

## Code examples

### Incorrect: the rule reports this

```js
this.foo = 1;   // Noncompliant
console.log(this.foo); // Noncompliant

function MyObj() {
  this.foo = 1; // Compliant
}

MyObj.func1 = function() {
  if (this.foo == 1) { // Compliant
    // ...
  }
}
```

### Correct: the rule accepts this

```js
foo = 1;
console.log(foo);

function MyObj() {
  this.foo = 1;
}

MyObj.func1 = function() {
  if (this.foo == 1) {
    // ...
  }
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/no-global-this": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-forced-browser-interaction](https://lintradar.dev/rules/sonarjs/no-forced-browser-interaction.md): Forced browser interactions should not bypass actionability checks
- [sonarjs/no-globals-shadowing](https://lintradar.dev/rules/sonarjs/no-globals-shadowing.md): Special identifiers should not be bound or assigned
- [sonarjs/no-floating-point-equality](https://lintradar.dev/rules/sonarjs/no-floating-point-equality.md): Floating point numbers should not be tested for equality
- [sonarjs/no-gratuitous-expressions](https://lintradar.dev/rules/sonarjs/no-gratuitous-expressions.md): Boolean expressions should not be gratuitous
- [sonarjs/no-fixed-wait-in-tests](https://lintradar.dev/rules/sonarjs/no-fixed-wait-in-tests.md): Fixed waits should not be used in tests
- [sonarjs/no-hardcoded-ip](https://lintradar.dev/rules/sonarjs/no-hardcoded-ip.md): IP addresses should not be hardcoded

---

Web page: https://lintradar.dev/rules/sonarjs/no-global-this. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
