Terry  Tremblay

Terry Tremblay


null and ES2020: Optional Chaining and Null Coalescing

When Javascript developers think of ECMA, they reference the changes made in 2015 that improved syntax and added new features to the language. However, ECMA still updates the language! And earlier this year, they released ES11, with two of my new favorite tools: Optional Chaining and Null Coalescing!

A Brief History of null

null is a very interesting datatype. It is a primitive type, along with String, Number, Boolean, Symbol, and Undefined. (Side note: as of ES11, BigInt is now another primitive type!) Primitive types are immutable, while objects (including arrays), can be mutated.

However, null is the odd primitive out. When you use typeof on any datatype, you are given a reasonable and expected response: typeof 3 is a Numbertypeof {foo: "bar"} is an Object , etc. Due to the way Javascript was developed, null is considered an Object when typeof is applied to it. Check out this brilliant article by Dr. Axel Rauschmayer for more detail!

null is a falsey value, is loosely equal to undefined (undefined == null // TRUE), and, most importantly represents the intentional absence of any object value. When a value is not defined, it will always receive the value of undefined. When Javascript developers declare a variable but want it to be empty, they will set the value to nullnull is never assigned by Javascript, but only by developers; it is a useful tool to give a value of null if it does not have any meaningful data. Getting a value of null means that your variable is declared and does exist, automatically informing you that you don’t have to adjust variable declaration, hoisting, or scope issues: the value is usable, just void of any information.

Undefined versus Null: an empty toilet paper dispenser and a toilet paper tube on a dispenser with no toilet paper.

#javascript #ecmascript-2020 #null #nullish-coalescing #optional-chaining

null and ES2020: Optional Chaining and Null Coalescing