close

no-global-assign

Added in v0.3.4

Configuration

PresetConfigured Value
✅ js.configs.recommended"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-global-assign': 'error',
    },
  },
]);

Rule Details

Disallows assignments to native objects or read-only global variables. Built-in globals such as Object, Array, String, Number, Math, JSON, undefined, NaN, Infinity, and others should not be reassigned, as doing so can cause unexpected behavior throughout the application.

Examples of incorrect code for this rule:

String = 'hello';
Array = 1;
undefined = true;
NaN++;

Examples of correct code for this rule:

var x = String(123);
var y = new Array(1, 2, 3);

// Shadowed by local declaration
var String;
String = 'hello';

// Shadowed by function parameter
function foo(Array) {
  Array = 1;
}

Globals declared through languageOptions.globals or a /* global */ comment carry their own access level: a readonly name is reported like a built-in, and a writable name may be reassigned — including a built-in whose declaration lifts the default.

Examples of incorrect code with globals: { BUILD_ID: 'readonly' }:

BUILD_ID = 'dev';

Examples of correct code with globals: { Object: 'writable' }:

Object = {};

Options

This rule accepts an optional object with an exceptions property, which is an array of global names that should be allowed to be reassigned:

{
  "no-global-assign": ["error", { "exceptions": ["Object"] }]
}

Original Documentation