Understanding “Assignment to Read-Only Global (e.g., undefined = 5)” in JavaScript
📌 Introduction
In JavaScript, some global variables such as undefined, NaN, and Infinity are read-only, meaning they cannot be reassigned. Attempting to assign a new value to them, like undefined = 5, results in an error in strict mode or fails silently in non-strict mode.
In this detailed guide, we will explore:
- What causes this error?
- Why does JavaScript prevent reassignment of global constants?
- How JavaScript handles these special values?
- How to fix this error correctly?
1️⃣ What Causes This Error?
JavaScript has built-in global properties that have immutable values, meaning they cannot be modified. These include:
undefinedNaN(Not a Number)Infinity
If you try to assign a new value to one of these properties, JavaScript either:
- Throws an error (in strict mode).
- Fails silently (in non-strict mode).
🔹 Example: Assigning a Value to undefined
undefined = 5;
console.log(undefined); // Still remains undefined
✅ Output (Non-Strict Mode):
undefined
🔴 Error (Strict Mode – use strict)
"use strict";
undefined = 5; // TypeError: Assignment to constant variable.
🔹 Example: Assigning a Value to NaN
NaN = "Not a Number";
console.log(NaN); // Still remains NaN
🔴 Error in Strict Mode
"use strict";
NaN = "Not a Number"; // TypeError: Assignment to constant variable.
2️⃣ Why Are Some Global Properties Read-Only?
JavaScript follows ECMAScript standards, where certain values must remain unchangeable to prevent unintended side effects in programs.
🔸 Reasons for Read-Only Global Variables
- Prevents accidental modifications
- If developers could change
undefined, it would break many JavaScript programs.
- If developers could change
- Ensures consistency in code execution
undefinedshould always mean “not assigned”.
- Improves security and debugging
- Allowing modifications could introduce hard-to-find bugs.
🔹 Example of a potential issue if undefined were mutable:
undefined = "Oops!"; // This would break many functions checking for undefined values
function checkUndefined(x) {
return x === undefined;
}
console.log(checkUndefined()); // Should return true, but would break if undefined was reassigned
3️⃣ How JavaScript Prevents Reassignment
🔹 undefined, NaN, and Infinity are Non-Writable
console.log(Object.getOwnPropertyDescriptor(window, "undefined"));
✅ Output:
{
value: undefined,
writable: false,
enumerable: false,
configurable: false
}
🔹 Key Takeaways:
writable: false→ Cannot be reassigned.configurable: false→ Cannot be deleted or redefined.
4️⃣ Fixing the Error
✅ Solution 1: Avoid Assigning Values to Read-Only Globals
Simply do not assign values to undefined, NaN, or Infinity.
"use strict";
undefined = 5; // ❌ Wrong - This will throw an error
🔹 Instead, use a different variable:
let myUndefined = 5; // ✅ Correct
✅ Solution 2: Use let or const Instead of Reassigning Globals
Instead of modifying undefined, use a variable:
let myVar = undefined; // ✅ Correct way
console.log(myVar); // undefined
✅ Solution 3: Check If a Variable is Undefined Correctly
Instead of relying on undefined directly, use:
if (typeof myVar === "undefined") {
console.log("Variable is undefined");
}
🔹 Why?
- Even if someone mistakenly defines
undefined = 5, this check still works.
✅ Solution 4: Use Object.getOwnPropertyDescriptor() to Verify Read-Only Properties
If you’re unsure whether a property is read-only, you can check its attributes:
console.log(Object.getOwnPropertyDescriptor(globalThis, "undefined"));
🔹 Output:
{
value: undefined,
writable: false,
enumerable: false,
configurable: false
}
Since writable: false, we cannot modify it.
5️⃣ Common Mistakes to Avoid
❌ Mistake 1: Using undefined in Comparisons Without typeof
if (someVar === undefined) { // May break if `undefined` is reassigned
console.log("Variable is undefined");
}
✅ Fix:
if (typeof someVar === "undefined") {
console.log("Variable is undefined");
}
❌ Mistake 2: Trying to Modify NaN or Infinity
NaN = 100; // ❌ Error in strict mode
Infinity = -1; // ❌ Error in strict mode
✅ Fix:
- Use a separate variable instead of modifying them.
❌ Mistake 3: Expecting undefined to Be Mutable
undefined = "Changed";
console.log(undefined); // Still undefined
✅ Fix: Do not assign values to undefined.
6️⃣ Summary & Best Practices
| Concept | Key Takeaway |
|---|---|
undefined, NaN, Infinity | Are read-only global constants |
| Assigning new values | Fails silently in non-strict mode, throws an error in strict mode |
Object.getOwnPropertyDescriptor() | Helps check if a property is writable or configurable |
| Checking undefined values | Use typeof myVar === "undefined" instead of direct comparison |
| Avoid modifying globals | Use let or const to store custom values |
🎯 Final Takeaways
✅ Never assign values to undefined, NaN, or Infinity.
✅ Use typeof to check for undefined variables safely.
✅ Use Object.getOwnPropertyDescriptor() to check property mutability.
✅ Enable "use strict" to catch these errors early.
Would you like more examples on best practices?
