To Undefined or not to Undefined:
The early to mid-1990s was an important time for the internet. Key players like Netscape and Microsoft were in the midst of browser wars, with Netscape’s Navigator and Microsoft’s Internet Explorer going head to head.
vs
In May 1995, a Netscape programmer named Brendan Eich (Hired in February) developed a new scripting language (POC) in just 10 days. It was originally named Mocha, but quickly became known as LiveScript and, later, JavaScript.
We aimed to provide a “glue language” for the Web designers and part time programmers who were building Web content from components such as images, plugins, and Java applets. We saw Java as the “component language” used by higher-priced programmers, where the glue programmers—the Web page designers—would assemble components and automate their interactions using [a scripting language].
What could go wrong?
foo = "bar"
myVariable = 1
if (true) {
myVar = 2
}
console.log(myVar)
return
{ status: true };
return; // undefined
return {
status: true
}
Any value that's not on this "falsy" list is "truthy."
The specific list of falsy values in JavaScript is as follows
var a = "42";
var b = 42;
a == b; // true
a === b; // falsevar a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
a == c; // true
b == c; // true
a == b; // falseSo you should only ever set variables to null. Never to undefined.
...UNLESS you are working with existing code or a specific api or library explicitly checking for undefined, then of course to force the behavior you want you’d have to make an exception.