Home > Back-end >  Does the "undefined" data type have only one value in JavaScript?
Does the "undefined" data type have only one value in JavaScript?

Time:07-09

The JavaScript documentation says that the Null data type have only one value:

The Null type has exactly one value: null. See null and Null for more details.

But it doesn't say that the undefined data type have only one value:

A variable that has not been assigned a value has the value undefined. See undefined and Undefined for more details.

So does the undefined data type have only one value or more than one value?

CodePudding user response:

There is exactly one value of the type undefined. There are multiple things that can evaluate to undefined, like an unassigned variable, missing property, or the void operator, among others:

let x;
const obj = { };
const voidReturn = void 0;

console.log(typeof x, x);
console.log(typeof obj, obj);
console.log(typeof voidReturn, voidReturn);


For clarity there is a value undefined but there is also also the global property undefined. Unlike null it is actually part of window. So, if the literal undefined is used in code, it refers to that property:

console.log("undefined" in window)

In the past it was possible to directly change it window.undefined = 42; which would then yield a strange result if used with undefined === void 0. The global property has been made immutable to prevent this, however, it is still technically possible to work around the immutability and provide your own value for undefined if you shadow the property:

function foo(undefined) {
  console.log(undefined === 42);
  console.log(typeof undefined);
  console.log(undefined === void 0);
}

foo(42);

By comparison the null is a language keyword and it cannot be overwritten in any fashion.

CodePudding user response:

When in doubt, go to the specification: :-)

The Undefined Type

The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

So: The Undefined type has only one value.


I should note that there are two ways for a variable to get that value, so perhaps that's the confusion. The two ways are:

  1. Because the variable has never had anything assigned to it (as the specification notes).
  2. Because the variable has explicitly had undefined assigned to it (directly or indirectly).

Example:

let a;
// `a` has the value `undefined` because it has never been assigned a value
console.log(typeof a); // undefined

let b = undefined;
console.log(typeof b); // undefined
let c = void 0;
console.log(typeof c); // undefined
let d = (() => { })();
console.log(typeof d); // undefined
const obj = {};
let e = obj.propertyThatDoesntExist;
console.log(typeof e); // undefined

b, 'c', 'd', and e have the value undefined because they've had that value assigned to them:

  • b is assigned undefined by using the global undefined variable
  • c is assigned undefined by using the result of the void operator, which is always undefined
  • d is assigned undefined because it's assigned the return value of a function that never issues a return statement with a value; the function therefore returns the value undefined
  • e is assigned undefined because it's assigned the value resulting from a property accessor expression (obj.propertyThatDoesntExist) for a property that doesn't exist; the result of a property access on a property that doesn't exist is undefined.

That's a reasonable list of ways variable may be assigned undefined; I'm sure there are more, it's just a sampling.

See also VLAZ's subsequent answer, which has some really good info in it. For instance, he makes the point that null is a keyword, but undefined is just a global variable.

CodePudding user response:

A good question! Taken from the book you don't know Javascript - undefined is itself a value used when there is no value. It's a property in the global object.

For example, quoting from the book here:

var a;

typeof a; // "undefined"

var b = 42;
var c;

// later
b = c;

typeof b; // "undefined"
typeof c; // "undefined"

a is undefined because we have not given it a value, but even in the case where we define the value of a variable as another undefined variable, it is still undefined. In this case, b and c are likely pointing to that property in the global object.

I recommend reading the Mozilla docs and the YDKJS chapter on types and values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

YDKJS types

  • Related