JS Built-ins
StrictModebeginner

Undeclared Variables in Strict Mode

Undeclared Variables in Strict Mode

In strict mode, assigning to an undeclared variable throws a ReferenceError. Without strict mode, it silently creates a global variable.

Challenge: Write testUndeclared() that:

  1. Uses eval to run code in strict mode that assigns to an undeclared variable
  2. Wraps it in try/catch
  3. Returns true if a ReferenceError is caught, false otherwise
πŸ’‘ Hints (2)
  • β€’ Use eval() to run code in strict mode
  • β€’ Wrap the eval in try/catch to catch the ReferenceError