The basics of parameter handling in JavaScript are simple, advanced tasks require manual work. This blog post first looks at the basics and then covers advanced topics.
Let’s start with a simple rule: the normal equality operators == and != are so problematic that you should always use strict equality (=== and !==). Some people say that there are exceptions to this rule, I disagree [2]. Keeping this rule in mind, we can now take a look at what is strange about ==, without burdening our minds unnecessarily.
Most programming languages have only one value for “no value” or “empty reference”. For example, that value is null in Java. JavaScript has two of those special values: undefined and null. They are basically the same (something that will change with ECMAScript 6, as will be explained in the last post of this series), but they are used slightly differently.
JavaScript is very tolerant when it comes to accepting values. For example, everywhere it expects a number, it does not reject values from other types, but tries to convert them:
> '5' - '2' 3 > '5' * '2' 10Automatic conversion to boolean is rarely problematic and often useful. It is covered here as a preparation for later – we’ll use it to work around quirks. Automatic conversion to string, however, can cause problems.
Object.prototype.valueOf = function () { throw new Error('Use an explicit toString'); };
node --v8-options | grep harmony