3

For example imagine the following expression:

undefined = "whatever";

The value of undefined isn't change by this, but it doesn't produce any error or exception whatsoever either, neither in Firefox, Chrome, Edge nor IE11.

Actually the value of the expression is even the assigned value instead of undefined:

var x = (undefined = "whatever");

Now x holds the value "whatever".

This seems weird on the one hand, but worse, a source for bugs on the other hand because nobody should even attempt to redefine undefined, and typos that result in doing so should be caught by the engine. Why would that fly?

Compare to the behavior when trying to redefine other keywords, for example:

for = 12345;

This yields "Uncaught SyntaxError: Unexpected token =" as it rightly should.

user777
  • 906
  • 5
  • 16
  • 2
    It does in strict-mode. – Sebastian Simon Sep 12 '16 at 13:56
  • Why not in every mode? – user777 Sep 12 '16 at 13:57
  • Because it does. That's simply what the language designers decided it should. They then changed their mind in an around [ECMAScript 5 and introduced strict mode](http://stackoverflow.com/a/1335881/542251). They needed a flag like this to maintain backwards compatibility – Liam Sep 12 '16 at 14:01
  • 1
    `for` is different as it is a keyword, whereas `undefined` is not. – Sebastian Simon Sep 12 '16 at 14:01
  • Possible duplicate of [What does "use strict" do in JavaScript, and what is the reasoning behind it?](http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) – Liam Sep 12 '16 at 14:01
  • 1
    What is `undefined` if it's not a keyword? It's obviously not a variable. – user777 Sep 12 '16 at 14:03
  • 1
    It’s three things: a property on the `window` object, a value and a type. – Sebastian Simon Sep 12 '16 at 14:04

2 Answers2

1

Because js has to be backwards compatible and at some time it was introduced that way. To tackle this problem strict mode was introduced. While using strict mode undefined = "whatever" will throw an error.

giggo1604
  • 481
  • 1
  • 5
  • 14
  • @Liam you mean < ECMAScript 5 – user777 Sep 12 '16 at 14:07
  • @Liam No. "it" being the possibility to use undefined as the left hand side of an assignment expression was introduced before ES5, that's the actual problem that ES5 simply has to preserve to maintain backwards-compatibility, but tried to conquer at least by introducing strict-mode – user777 Sep 12 '16 at 14:11
1

Reading from MDN:

While it is possible to use it as an identifier (variable name) in any scope other than the global scope (because undefined is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.

(function () {
  var undefined = 'foo';
  console.log(undefined, typeof undefined);
})();
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • 1
    @user777 ES5 is backwards-compatible. No browser wants to break the web. – Bergi Sep 12 '16 at 14:09
  • Thanks, makes sense. Hence the addition of strict-mode. – user777 Sep 12 '16 at 14:09
  • This even works in strict mode! Even more WTF?? Sorry, I don't understand why that would work in strict mode. Any reason? – user777 Sep 12 '16 at 14:17
  • Right, but WHY would they allow it even in strict-mode?? The mode that was purposefully added to get rid of brainfarts from the old spec, which couldn't just be removed from standard mode without possibly breaking the web. But it could very well be removed in strict-mode – user777 Sep 12 '16 at 14:32
  • @user777 Reading in MDN about Strict mode I can only understand: undefined is not a keyword, it could be used for very, very, very resticted purposes so it's possible to declare a variable with that name. This is the only idea I have reading from MDN. Let's imagine to change the meaning of undefined for special test purposes..... Sorry, but I do not have other ideas to share. Thank you very much. – gaetanoM Sep 12 '16 at 14:39
  • Aliright, thanks geatanoM. It's weird and smells brainfarty, but I guess it's just the way it is... – user777 Sep 12 '16 at 14:41