Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is undefined not writable in JavaScript?

According to MDN's documentation on undefined:

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.

One of the property attributes of undefined is that is not writable.

But if I do:

var undefined = 'hello';  var test = undefined;  console.log(typeof test);  //string

Does that mean that I can overwrite the value of undefined? What happens if someone does that? Should JavaScript warn about that?

like image 233
pmiranda Avatar asked Dec 26 '19 15:12

pmiranda


People also ask

Is undefined in JavaScript?

Undefined is also a primitive value in JavaScript. A variable or an object has an undefined value when no value is assigned before using it. So you can say that undefined means lack of value or unknown value. undefined is a token.

Why does JavaScript print undefined?

In JavaScript if a variable has been declared, but has not been assigned a value, is automatically assigned the value undefined . Therefore, if you try to display the value of such variable, the word "undefined" will be displayed.

Is NaN a keyword?

NaN is not a keyword, but it is rather a built-in property of the global object, and as such may be replaced (like undefined , but unlike the keyword this or the literals true , false , and null ). You can test if a value is NaN with the isNaN() function.

Can JavaScript numbers be undefined?

JavaScript assigns 'undefined' to any object that has been declared but not initialized or defined. In other words, in a case where no value has been explicitly assigned to the variable, JavaScript calls it 'undefined'.


1 Answers

A removed comment by Amy gives the solution. You are creating a variable named undefined, and it doesn't work if you do your snippets in the global scope:

var undefined = 'hello';  var test = undefined;  console.log(typeof test);

But it effectively works if you do it in a local scope where undefined doesn't refer to the global variable anymore:

(()=>{    var undefined = 'hello';    var test = undefined;    console.log(typeof test);  })()

To prevent this mistake, you can use the 'use strict'; directive:

'use strict';  var undefined = 'hello';  var test = undefined;  console.log(typeof test);

If you are writing code where you can't control where it will be executed (e.g. library, embed, etc) it is a good pattern to use a IIFE which makes it so you can guarantee that undefined will always be correct/usable. Here is an example:

(function(undefined){    // undefined will equal `undefined` because we didn't pass in an argument to the IIFE        console.log(undefined); // always `undefined`, even if undefined !== `undefined`outside of the IIFE scope  })(); // No argument supplied

All my tests were made using Firefox 72.0b10 (64 bits) on Ubuntu, and the result for the first snippet may differ on an older browser.

like image 164
jonatjano Avatar answered Oct 21 '22 04:10

jonatjano