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:
undefined
NaN
(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
undefined
should 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?