undefined vs. null in JavaScript

JavaScript distinguishes "undefined" and "null" in something of the way that Ren-C distinguishes unset and null variables. An undefined variable is one you haven't assigned yet, NULL is one you've set to NULL.

// Chrome Console Session
> asdf
VM66:1 Uncaught ReferenceError: asdf is not defined

> typeof asdf
<- "undefined"

> asdf = null
<- null

> asdf
<- null

> typeof asdf
<- "object"

(If you're curious why the typeof null is an object in JavaScript: "Because the spec says so. This is generally regarded as a mistake.". The move of not having an answer for the value of the type, e.g. saying the answer to type of null is null - is much better)

This is how they deal with assignments:

> function nothing() { return; }

> let x = nothing()
<- undefined

> if (x) { console.log("it doesn't error even on dereference"); }
<- undefined

> if (!x) { console.log("because it's falsey"); }
because it's falsey
<- undefined

That's extremely forgiving...but not the direction we want to go in the "undefined" case. There's a very narrow exception made with SET-WORD! for assignments from a QUASI! to generate isotopes, and that's it.

Why We Allow *Direct* Isotope Assignments (e.g. variable: ~)

1 Like