Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Does the "undefined" data type have only one value in JavaScript?

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!