1

Reading You-dont-know-Js at this chapter: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch3.md

there's a code snippet:

undefined = true; // setting a land-mine for other code! avoid!

(function IIFE( undefined ){

 var a;
 if (a === undefined) {
  console.log( "Undefined is safe here!" );
 }

})();

as for the immediately executed function, I know that the argument is undefined, a is also undefined, thus a===undefined.

my question is that, how is it possible to assign a value to undefined? As I understand, undefined is a primitive type in js, then what is the meaning of undefined = true;?

Additionally, to test my understanding, I changed this code a little:

undefined = 5; // set undefined to an integer? how can it be?

(function IIFE( undefined ){

 var a;
 if (a === undefined) {
  console.log( "Undefined is safe here!" );
 }

})(undefined);  //still execute console.log function

and the result remains same

Lake_Lagunita
  • 521
  • 1
  • 4
  • 20

1 Answers1

2

how is it possible to assign a value to undefined? As I understand, undefined is a primitive type in js, then what is the meaning of undefined = true;?

You are right, undefined is a primitive type, but it isn't a literal.

When you access undefined, you are accessing a property in the global object.

Before ES5, this property was writable, and it could be overridden.

But since ES5, it was defined as not writable, and now it cannot be changed.

Not being a literal like null, true or false, makes possible to use undefined as a binding name for a variable, a parameter on a function or the name of a function, but not in the global scope. It will clash with the built-in undefined.

(function () {
  'use strict';  
  const undefined = 'some value';  
  console.log(undefined); // "some value"  
})();
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • so for the second code snippet, when "undefined = 5", actually undefined is not assigned to 5(because it is not writable) ? – Lake_Lagunita Oct 16 '19 at 16:26
  • @LeonloveKaren in strict mode, ES5+, `"use strict"; undefined = 5;` throws a TypeError. In non strict, it just silently does nothing. – ASDFGerte Oct 16 '19 at 16:30
  • "*makes it possible to use undefined as a binding name for a variable or function.*" just to clarify, this cannot be done in global scope, since it's still clashing with the `window` property, but you can have `var undefined` in function scope, for example. – VLAZ Oct 16 '19 at 16:32
  • @ASDFGerte If I was the code inventor, I would prefer throw error at any situation, no matter strict mode is. – Lake_Lagunita Oct 16 '19 at 16:35
  • @VLAZ, added clarification and example, tks! – Christian C. Salvadó Oct 16 '19 at 16:36
  • 1
    JS has a lot, and i mean a LOT of legacy issues. Writing code in non-strict is imho ill advised anyways. While you are at "weird JS things in a browser", you can try `typeof document.all === "undefined" && document.all !== undefined` – ASDFGerte Oct 16 '19 at 16:37
  • @ASDFGerte, yes, the assignment it will fail, and it will crash under strict mode. Meaning that if you are using ES6 modules, which are strict by default, it will just crash. – Christian C. Salvadó Oct 16 '19 at 16:37
  • 1
    @ASDFGerte my favourite legacy issue is `typeof null === "object"`. – VLAZ Oct 16 '19 at 16:40