JavaScript quirk 4: unknown variable names create global variables

[This post is part of a series on JavaScript quirks.]


Normally, JavaScript automatically creates a global variable if you use an unknown variable name:


> function f() { foo = 123 }
> f()
> foo
123

Thankfully, you get a warning in ECMAScript 5 strict mode [1]:

> function f() { 'use strict'; foo = 123 }
> f()
ReferenceError: foo is not defined

Reference:

  1. JavaScript’s strict mode: a summary


Comments

Popular posts from this blog

Steve Lopez and the Importance of Newspapers

A Treasure Hunt Without The Treasure

Drop a ping-pong ball in the clown’s mouth