var undefined = "raspberry";
function eatFruit (fruit) {
if (fruit === undefined) {
fruit = "strawberry";
}
...
}
eatFruit("raspberry"); var undefined = "raspberry";
function eatFruit (fruit) {
if (fruit === undefined) {
fruit = "strawberry";
}
...
}
eatFruit("raspberry");I'm sure this used to work, a few years ago... but I may be getting old.
$ node
> var bar = undefined = "foo";
undefined
> bar
'foo'
> undefined
undefined
>In ES5 these properties are defined as [[Writable]]: false, which explains your behaviour.
You can however still create such variable in non-global scope - simply wrap that code in a function and it will work.
if (foo === void 0) ...
//or
var undef;
...
if (foo === undef) ...