What happens if we set the value to undefined?

What does this line do below?

undefined = 'A value'; 

If this does not change the value of undefined , then what happens behind the scenes?

+58
javascript undefined
Apr 09 '16 at 16:55
source share
3 answers

undefined is a property of a global object, i.e. is a variable in a global area. The initial value of undefined is the primitive value of undefined .

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

So this is just a variable, nothing special. Now to answer your questions:

  • undefined = 'A value'; trying to assign string 'A value' global variable undefined
  • In older browsers, the value changes, i.e. undefined === 'A value'; // true undefined === 'A value'; // true . In new browsers in strict mode, the operation results in an error.

You can test the following in the browser console (I use a modern browser here - Google Chrome):

 undefined = true; console.log(undefined); // undefined // in older browsers like the older Internet Explorer it would have logged true 

In the above example, the value undefined does not change. This is because (my attention):

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non - configurable, non-writable property according to the ECMAScript 5 specification.

In strict mode:

 'use strict'; undefined = true; // VM358:2 Uncaught TypeError: Cannot assign to read only property 'undefined' of object 
+53
Apr 09 '16 at 16:59
source share

Unlike true , 123 or null , undefined not a literal . This means that using an undefined identifier is not a reliable way to get an undefined value . Instead, you can use the void operator, for example. void 0 .

By default, undefined a property of a global object is defined, that is, a global variable. Prior to ECMAScript 5, this property was writable, therefore

 undefined = "A value"; 

replaced the value of window.undefined , assuming that it is not obscured by the local variable. Then, if you used "A value" === undefined , you would get true . And void 0 === undefined will create false .

ECMAScript 5 has changed this behavior, and now the property is not writable or customizable. Therefore, undefined assignments will be ignored in non-strict mode and will throw a strict mode exception. Under the hood

  • undefined = "A value"; is a simple appointment
  • This uses PutValue to put the value "A value" in the link with the base of the global object, the link name is "undefined" and a strict flag if the job is done in strict mode.
  • It calls the internal [[Put]] method of the global object, passing "undefined" as the property name, "A value" as the value, and a strict flag as the throw flag.
  • It calls the [[DefineOwnProperty]] internal method of the global object, passing "undefined" , the property descriptor {[[Value]]: "A value"} and the throw flag as arguments.
  • It rejects, that is, throws a TypeError exception if the throw flag is true, otherwise returns false.

However, you can still declare local variables undefined :

 (function() { var undefined = "A value"; alert(undefined); // "A value"; })(); 
+24
Apr 09 '16 at 17:21
source share

I made a small POC with and without strict mode .

The effect is that if you do not use strict mode , everything will be fine. If you use strict mode , you will be pleased:

TypeError: cannot assign read-only property to 'undefined'

Now go to POC:

 "use strict" var c; if (c === undefined) { console.log("nothing happened") } undefined = "goofy" c = "goofy" if (c === undefined) { console.log("c is 'goofy' and it equal to undefined.. gosh.. we broke js") } 

Now, as I said, in strict mode you get a TypeError , and when you remove "use strict" script goes fine, and the output is just nothing happened .

I found this Q / A , which may be useful if you want to know more.

NOTE. I tested this code with Node.js

+3
Apr 09 '16 at 17:05
source share



All Articles