June 26th, 2024

The many faces of undefined in JavaScript

JavaScript's handling of undefined values, including null and falsy values, can confuse developers. TypeScript introduces nuances with function arguments and void type. Recommendations suggest treating undefined as null to avoid issues.

Read original articleLink Icon
The many faces of undefined in JavaScript

JavaScript presents various ways to represent undefined values, leading to confusion and potential code defects. The article discusses null as an optional value, undefined as a missing or actual value, and interpretative cases like falsy values and ReferenceError exceptions. It delves into TypeScript nuances with function arguments and the void type. The inconsistency in handling undefined values, such as treating undefined as equivalent to null, poses challenges in serialization and dictionary usage. Recommendations include avoiding falsy comparisons, defensively treating undefined as null, and minimizing distinctions between null and undefined to mitigate issues arising from the diverse representations of undefined in JavaScript. The evolution of the language and TypeScript's adherence to these conventions contribute to a complex system of handling missing values, potentially perplexing for developers accustomed to other programming languages.

Related

NPM and NodeJS should do more to make ES Modules easy to use

NPM and NodeJS should do more to make ES Modules easy to use

Boris Cherny discusses challenges with ES Modules in NodeJS and NPM, proposing solutions like simplifying file extensions, upgrading libraries, and phasing out CommonJS support in NodeJS to boost adoption rates.

New JavaScript Set Methods

New JavaScript Set Methods

New JavaScript Set methods introduced in major browsers like Firefox 127 offer efficient set operations without polyfills. These methods simplify tasks like finding intersections, unions, and subsets, enhancing working with unique collections.

A reckless introduction to Hindley-Milner type inference (2019)

A reckless introduction to Hindley-Milner type inference (2019)

Hindley-Milner type inference balances expressiveness and legibility in programming languages like Elm and Haskell. It enhances correctness by enforcing strict type checking, limiting coding practices for improved type safety.

How GCC and Clang handle statically known undefined behaviour

How GCC and Clang handle statically known undefined behaviour

Discussion on compilers handling statically known undefined behavior (UB) in C code reveals insights into optimizations. Compilers like gcc and clang optimize based on undefined language semantics, potentially crashing programs or ignoring problematic code. UB avoidance is crucial for program predictability and security. Compilers differ in handling UB, with gcc and clang showing variations in crash behavior and warnings. LLVM's 'poison' values allow optimizations despite UB, reflecting diverse compiler approaches. Compiler responses to UB are subjective, influenced by developers and user requirements.

JEP 401: Value Classes and Objects (Preview)

JEP 401: Value Classes and Objects (Preview)

JEP 401 introduces value classes and objects in Java, focusing on optimizing memory efficiency by distinguishing objects solely by their field values, not identity. This feature enhances performance for simple domain values.

Link Icon 12 comments
By @ivanb - 5 months
This was probably the single worst design decision of JavaScript. It is the only language I know of that has two "absent value" types. If null was a billion dollar mistake, then undefined adds an order of magnitude to the problem. There is absolutely no reason for undefined to exist.

We've generally adopted strict mode. I think it is now time to come up with and adopt a new no-undefined mode. And yes, it should break the standard library. It is worth it.

By @nanofus - 5 months
This was a nice read, well-written and informative!

I could add that in my mental model, the reason that an object property with an undefined value is different from an actually undefined property, is that since JS objects are basically key/value maps, there is a difference between a key existing with an undefined value and the key itself not existing at all.

By @kennu - 5 months
I never use null in Javascript, because I don't want attributes of type null, I just want attributes to either be present or not. This usually simplifies all designs and there is no need to think about null separately; it's treated in code an invalid value for an attribute.

The remaining caveat is that when serializing an object, attributes of type undefined need to be skipped, and some libraries get that wrong. So sometimes it is necessary to delete them from objects before serialization.

So in my view, things would be better if null didn't exist at all and if assigning an undefined value to an object attribute would actually delete the attribute from the object. Of course the language can't be changed now, but that is the philosophy I try to use in code design.

I do realize there is the use case of passing an update object e.g. with HTTP PUT/PATCH and wanting to make a distinction between "don't update an attribute" and "remove an attribute". In that case, null kind of makes sense as a designator to remove an attribute.

By @bogdan - 5 months
> void doesn’t introduce any new semantics to the language.

I'm sure the author is most likely aware of this plus the context is around TypeScript but I think it can still be misunderstood and if you haven't been around when IIFE were very common you might not be aware that:

`void` is a javascript operator and it does have semantic meaning, it evaluates an expression and returns undefined.

   const a = void 1;
   console.log(a); // undefined

see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
By @the_gipsy - 5 months
JavaScript, where "Null: the billion dollar mistake" is doubled by having yet another null-ish value.
By @aantthony - 5 months
There is also one more kind of undefined, which is undefined as a type: the string value "undefined" for `typeof undefined`
By @spoiler - 5 months
The TypeScript portion is a little bit wrong. Return position void in TypeScript means the value is unobserved by the caller, not that it's undefined. It's subtly different.
By @Maxion - 5 months
It never fails to amaze me how JavaScript still has so many inconsistencies and weird behavior.

My biggest pet peeve is the lack of decimal in the main language.

By @kaon_ - 5 months
One positive note: I think the name "undefined" is actually very fitting for what it represents.
By @jschrf - 5 months
Simple rule of thumb: never catch yourself typing the word "undefined" in a JS/TS codebase.

Easy.

By @tipiirai - 5 months
For me, the flexibility that comes from loose typing is one of the biggest strengths in JavaScript. For example, using loose equality (double equals) is truthful for both null and undefined, but false for 0.
By @Vanit - 5 months
Null is the sole case in js that should always be == checked. Null or undefined should never be === unless you're explicitly encoding a semantic difference in your types.